分析

threadlocal在线程间是隔离的,不共享,用于存储线程的变量

即使多个线程使用同一个ThreadLocal,也只能访问自己的属性

一个简单的例子

  • 线程1和线程2分别从同一个ThreadLocal中存取变量
  • 主线程没有存变量,也在获取
  • 可以发现,每个线程获取的都是自己存的
  • 如果没有存,则返回默认的,默认是空
public class ThreadLocalComplexTest {
    private final static ThreadLocal<String> THREAD_LOCAL = new ThreadLocal<String>() {
        @Override
        protected String initialValue() {//设置默认值
            return "Alex";
        }
    };

    private final static Random RANDOM = new Random();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            THREAD_LOCAL.set("Thread-T1");
            try {
                Thread.sleep(RANDOM.nextInt(1000));
                System.out.println(Thread.currentThread().getName() + THREAD_LOCAL.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread t2 = new Thread(() -> {
            THREAD_LOCAL.set("Thread-T2");
            try {
                Thread.sleep(RANDOM.nextInt(1000));
                System.out.println(Thread.currentThread().getName() + THREAD_LOCAL.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t2.start();
        Thread.sleep(RANDOM.nextInt(1000));
        System.out.println(Thread.currentThread().getName() + THREAD_LOCAL.get());

    }
}

模拟线程保险箱

  • 使用Map的形式模拟
  • Map的key始终是当前线程,value是泛型
public class ThreadLocalSimulator <T> {

    private final Map<Thread,T> threadTMap = new HashMap<>();

    public void set(T t){
        synchronized (threadTMap){
            threadTMap.put(Thread.currentThread(),t);
        }
    }
    public T get(){
        synchronized (threadTMap){
            if (threadTMap.containsKey(Thread.currentThread())){
                return threadTMap.get(Thread.currentThread());
            }
            //如果没有赋值
            return initialValue();
        }
    }

    public T initialValue() {
        return null;
    }

}