ThreadLocal: 每个线程自身的存储本地, 局部区域, 类似于容器, 每个线程都会在其中有一定存储空间
常用的方法 get/set/initialValue
官方建议为 private static
每个线程存储自己的数据, 更改不会影响其他线程
ThreadLocal 子类 InheritableThreadLocal:
继承上下文环境的数据
- public class my {
- //Integer 初始值为 null
- //private static ThreadLocal<Integer> threadlocal=new ThreadLocal<>();
- // 更改初始值需要创建 ThreadLocal 的子类重写 initialValue 或者使用 lambda(jdk8)
- private static ThreadLocal<Integer> threadlocal=new ThreadLocal <Integer>()
- {// 父类匿名内部类重写方法
- protected Integer initialValue()
- {
- return 200;
- }
- };
- //private static ThreadLocal<Integer> threadlocal=new ThreadLocal.withInitial(()->200);
- public static void main(String[]args) throws InterruptedException
- {
- // 获取值
- System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
- // 设置值
- threadlocal.set(99);
- System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
- new Thread(new test()).start();
- new Thread(new test()).start();
- }
- public static class test implements Runnable
- {
- public void run()
- {
- threadlocal.set((int)(Math.random()*100));
- System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
- }
- }
- }
每个线程自身的数据更改不会影响其他线程
- public class my {
- private static ThreadLocal<Integer> threadlocal=new ThreadLocal <Integer>()
- {// 父类匿名内部类重写方法
- protected Integer initialValue()
- {
- return 1;
- }
- };
- public static void main(String[]args) throws InterruptedException
- {
- for(int i=0;i<5;i++)
- {
- new Thread(new test()).start();
- }
- }
- public static class test implements Runnable
- {
- public void run()
- {
- // 修改不会影响其他线程
- Integer left=threadlocal.get();
- System.out.println(Thread.currentThread().getName()+"-->"+left);
- threadlocal.set(left-1);
- System.out.println(Thread.currentThread().getName()+"还剩下"+threadlocal.get());
- }
- }
- }
分析 ThreadLocal 运行环境:
- public class my {
- private static ThreadLocal<Integer> threadlocal=new ThreadLocal <Integer>()
- {// 父类匿名内部类重写方法
- protected Integer initialValue()
- {
- return 1;
- }
- };
- public static void main(String[]args) throws InterruptedException
- {
- new Thread(new test()).start();// 两个打印的内容在不同的线程里
- // 当 start() 之后, 线程才从 main 转换到其他线程, 所以构造器里的是 main 线程的
- }
- public static class test implements Runnable
- {
- public test()
- {
- threadlocal.set(100); // 属于 main 线程, 修改不会影响 start 内的线程
- System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
- }
- public void run()
- {
- System.out.println(Thread.currentThread().getName()+"还剩下"+threadlocal.get());
- }
- }
- }
子类 InheritableThreadLocal:
- public class my {
- private static ThreadLocal<Integer> threadlocal=new InheritableThreadLocal<>();
- public static void main(String[]args) throws InterruptedException
- {
- System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
- threadlocal.set(2);
- // 此线程由 main 线程开辟, 拷贝一份 main 线程的数据给此线程
- // 即将 main 线程里的 2 给此线程
- new Thread(()->{
- System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
- threadlocal.set(200);
- System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
- }).start();
- }
- }
来源: http://www.bubuko.com/infodetail-3161283.html