這樣做保證了我們在同一個資源裏面共享同一個用戶。
package com.newer.util;
import com.newer.pojo.User;
/***
* 管理用戶登錄,註冊,在線用戶
* @author Administrator
*
*/
public class UserManager {
// 線程局部變量 (user在所有線程之間共享)
private static final ThreadLocal<User> local=new ThreadLocal<User>();
/***
* 登錄
* @param user
* @param password
* @param role
*/
public static void login(String user,String password,String role){
local.set(new User(user,password,role));
}
/***
* 在線
* @return
*/
public static User getOnline(){
return local.get();
}
/***
* 註銷
*/
public static void logOut(){
local.set(null);
}
}