黑马程序员技术交流社区
标题:
ThreadLocal的问题
[打印本页]
作者:
wnmmp
时间:
2014-8-8 15:57
标题:
ThreadLocal的问题
正在学习《张孝祥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;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2