public classThreadLocalTest {private static ThreadLocal threadLocal = new ThreadLocal();public static voidmain(String[] args) {//启动两个线程
for (int i = 0; i < 2; i++) {new Thread(newRunnable() {
@Overridepublic voidrun() {//创建每个线程私有的变量
int data = new Random().nextInt(100);
System.out.println(Thread.currentThread().getName()+" has put data: "+data);//往local里面设置值
threadLocal.set(data);//获取自己线程的MyThreadLocalScopeDate实例对象
MyThreadLocalScopeDate myData =MyThreadLocalScopeDate.getThreadInstance();
myData.setName("name"+data);
myData.setAge(data);newA().get();newB().get();
}
}).start();
}
}static classA{public voidget(){int data =threadLocal.get();
System.out.println("A from "+Thread.currentThread().getName()+" has get data: "+data);
MyThreadLocalScopeDate myData=MyThreadLocalScopeDate.getThreadInstance();
System.out.println("A from "+Thread.currentThread().getName()+" has get MyThreadLocalScopeDate name: "+myData.getName()+" , age: "+myData.getAge());
}
}static classB{public voidget(){int data =threadLocal.get();
System.out.println("B from "+Thread.currentThread().getName()+" has get data: "+data);
MyThreadLocalScopeDate myData=MyThreadLocalScopeDate.getThreadInstance();
System.out.println("B from "+Thread.currentThread().getName()+" has get MyThreadLocalScopeDate name: "+myData.getName()+" , age: "+myData.getAge());
}
}
}class MyThreadLocalScopeDate{//单例模式
private MyThreadLocalScopeDate(){};//构造方法私有化
private static ThreadLocal map = new ThreadLocal();//封装MyThreadLocalScopeDate是线程实现范围内共享//思考AB两个线程过来的情况 自己分析 AB都需要的自己的对象 没有关系 所以不需要同步 如果有关系就需要同步了
public static /*synchronized*/MyThreadLocalScopeDate getThreadInstance(){
MyThreadLocalScopeDate instance=map.get();if(instance==null){
instance= newMyThreadLocalScopeDate();
map.set(instance);
}returninstance;
}privateString name;private intage;publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}
}