正在学习《张孝祥Java多线程与并发库高级应用视频教程》中关于ThreadLocal类及应用技巧。。。懵了,为什么不能用MyThreadScopeData单例?而是用这个ThreadLocal的单例?
class MyThreadScopeData {
private MyThreadScopeData() {
}
public static/* synchronized */MyThreadScopeData getThreadInstance() {
MyThreadScopeData instance = map.get();
if (instance == null) {
instance = new MyThreadScopeData();
map.set(instance);
}
return instance;
}
private static ThreadLocal<MyThreadScopeData> map = new ThreadLocal<MyThreadScopeData>();
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
|
|