内容来自,张孝祥老师的张孝祥-Java多线程与并发库高级应用》视频教程
package com.lipeng;
public class MyThreadLocalTest2 {
private final static MyThreadLocal<String> myThreadLocal=new MyThreadLocal<String>();
/**
* @param args
*/
public static void main(String[] args) {
new Thread(){
public void run() {
ThreadLocalData.getThreadLocalData().setAge(12);
ThreadLocalData.getThreadLocalData().setName("张三");
System.out.println(Thread.currentThread().getName()+"----------------"+A.getName());
System.out.println(Thread.currentThread().getName()+"----------------"+B.getName());
};
}.start();
new Thread(){
public void run() {
ThreadLocalData.getThreadLocalData().setAge(18);
ThreadLocalData.getThreadLocalData().setName("李四");
System.out.println(Thread.currentThread().getName()+"----------------"+A.getName());
System.out.println(Thread.currentThread().getName()+"----------------"+B.getName());
};
}.start();
}
static class A
{
public static String getName()
{
return ThreadLocalData.getThreadLocalData().getName();
}
public static void setName(String name)
{
myThreadLocal.set(name);
}
}
static class B
{
public static String getName()
{
return ThreadLocalData.getThreadLocalData().getName();
}
public static void setName(String name)
{
myThreadLocal.set(name);
}
}
}
class ThreadLocalData
{
private static ThreadLocal<ThreadLocalData> threadLocal=new ThreadLocal<ThreadLocalData>();
private String name;
private int age;
private ThreadLocalData(){ //不提供public的构造方法,只提供一个静态工厂方法,此方法返回与当前线程有关的当前类的实例对象。
}
public static ThreadLocalData getThreadLocalData()
{
ThreadLocalData threadLocalData=threadLocal.get();
if(threadLocalData==null)
{
threadLocalData=new ThreadLocalData();
threadLocal.set(threadLocalData);
}
return threadLocalData;
}
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;
}
}
<span style="white-space:pre"> </span>我们在获取一个与线程绑定的对象时,不希望看到ThreadLocal,而是直接得到与当前线程绑定的对象,在这里,将线程绑定的类的构造方法private,提供一个静态方法,使得要获得这个类的对象,必须通过这个方法,而这个方法是从ThradLocal中get到的即使当前线程绑定的实例。