InheritableThreadLocal代码

public class InheritableThreadLocal<T> extends ThreadLocal<T> {protected T childValue(T parentValue) {return parentValue;}ThreadLocalMap getMap(Thread t) {return t.inheritableThreadLocals;}void createMap(Thread t, T firstValue) {t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);}
}

测试代码

import java.util.concurrent.atomic.AtomicInteger;/*** Created by hujunzheng on 2017/6/23.*/class ThreadCount {private static final AtomicInteger nextId = new AtomicInteger(0);private static final ThreadLocal<Integer> threadCount =ThreadLocal.withInitial(() -> nextId.getAndIncrement());//返回的是重写了ThreadLocal initialValue()方法的ThreadLocal.SuppliedThreadLocal对象public static int get() {return threadCount.get();}
}class ThreadSign {private static final AtomicInteger nextId = new AtomicInteger(0);private static final ThreadLocal<Integer> threadCount = new InheritableThreadLocal<Integer>() {@Overrideprotected Integer initialValue() {return nextId.getAndIncrement();}};public static int get() {return threadCount.get();}
}public class ThreadLocalTest {public static void testThreadLocal() {for (int i = 0; i < 5; ++i) {int pi = i;new Thread(() -> {System.out.println(Thread.currentThread().getName() + " count->" + ThreadCount.get());for (int j = 0; j < 5; ++j) {new Thread(() -> System.out.println("       " + Thread.currentThread().getName() + " count->" + ThreadCount.get()), "cthread" + j + " pthread" + pi).start();}}, "pthread" + i).start();}}public static void testInheritableThreadLocal() {for(int i=0; i<5; ++i) {int pi = i;new Thread(() -> {System.out.println(Thread.currentThread().getName() + " sign->" + ThreadSign.get());for(int j=0; j<5; ++j) {new Thread(() -> System.out.println("       " + Thread.currentThread().getName() + " sign->" + ThreadSign.get()), "cthread" + j + " pthread" + pi).start();}}, "pthread" + i).start();}}public static void main(String[] args) {
//        testThreadLocal();
        testInheritableThreadLocal();}
}

测试结果

  分别为testThreadLocal() 和 testInheritableThreadLocal() 测试结果。

    

  比较后,看到ThreadLocal里的值,子线程里不能获得;InheritableThreadLocal里的值,子线程可以获得。

  

跟踪一下代码

  

参考

  Java8增加功能--Effectively final 功能

转载于:https://www.cnblogs.com/hujunzheng/p/7069223.html

ThreadLocal和InheritableThreadLocal使用相关推荐

  1. 每日一博 - ThreadLocal VS InheritableThreadLocal VS TransmittableThreadLocal

    文章目录 ThreadLocal 核心API ThreadLocal类 源码分析 set get remove 缺陷 InheritableThreadLocal 源码解析 局限性 Transmitt ...

  2. ThreadLocal 和 InheritableThreadLocal

    在学习ThreadLocal之前,建议先了解Java中的4种引用 一.先看一下Thread,ThreadMap,ThreadLocal的关系 Thread中持有一个ThreadLocalMap ,这里 ...

  3. 【高并发】ThreadLocal、InheritableThreadLocal

    文章目录 1.概述 2.需要解决的问题 ThreadLocal InheritableThreadLocal 1.概述 转载:添加链接描述 2.需要解决的问题 我们还是以解决问题的方式来引出Threa ...

  4. 多线程-ThreadLocal,InheritableThreadLocal

    ThreadLocal 变量值得共享可以使用public static变量的形式,所有的线程都使用同一个public static变量.如果想实现每一个线程都有自己的共享变量该如何解决呢?JDK中提供 ...

  5. 【多线程编程】--ThreadLocal、InheritableThreadLocal(ITL)、TransmittableThreadLocal(TTL)解析

    目录 一.前言 二.ThreadLocal 2.1.为什么会用到ThreadLocal(ThreadLocal应用场景) 2.2.ThreadLocal实现原理 2.3.ThreadLocalMap ...

  6. ThreadLocal与InheritableThreadLocal源码解析

    ThreadLocal 变量值的共享可以使用public static变量的形式实现,所有的线程都使用同一个public static变量,那如何实现每一个线程都有自己的变量呢?JDK提供的Threa ...

  7. JUC第六讲:ThreadLocal/InheritableThreadLocal详解/TTL-MDC日志上下文实践

    本文是JUC第六讲:ThreadLocal/InheritableThreadLocal详解.ThreadLocal无论在项目开发还是面试中都会经常碰到,本文就 ThreadLocal 的使用.主要方 ...

  8. 【高并发】ThreadLocal学会了这些,你也能和面试官扯皮了!

    来自:冰河技术 前言 我们都知道,在多线程环境下访问同一个共享变量,可能会出现线程安全的问题,为了保证线程安全,我们往往会在访问这个共享变量的时候加锁,以达到同步的效果,如下图所示. 对共享变量加锁虽 ...

  9. 获取返回值作为变量_解决多线程间共享变量线程安全问题的大杀器——ThreadLocal...

    微信公众号:Zhongger 我是Zhongger,一个在互联网行业摸鱼写代码的打工人! 关注我,了解更多你不知道的[Java后端]打工技巧.职场经验等- 上一期,讲到了关于线程死锁.用户进程.用户线 ...

最新文章

  1. Linux下du加强版,灵活快速定位硬盘使用情况,无需安装
  2. attribute 'groups' of type 'int' is not usable in a script method
  3. 会计的疑惑--BigDecimal的秘密
  4. sql server之数据库语句优化
  5. loginButton.setOnClickListener(this);这里为什么要用this呢?
  6. Flex与.NET互操作(七):了解FluorineFx的环境配置(远程对象、网关、通道、目的地)...
  7. java重命名package_AndroidStudio怎么重命名java目录下的包名(如cn.zsn.app)
  8. php mysql 性能测试工具下载_MySQL压力测试工具_MySQL
  9. 想开公司没钱租用办公地,注册地址怎么办?
  10. NOIP2018普及T2暨洛谷P5016 龙虎斗
  11. UAV进阶论文和书籍
  12. 怎么恢复苹果内置的计算机,苹果自带软件删了怎么恢复原状
  13. 【阿里巴巴】学习Java在面试过程中跳槽成功的心得总结
  14. Hystrix php,详解 hystrix-go 使用与原理
  15. 揭明星工作室待遇:助理3000经纪人30万
  16. X-Powered-By: Servlet/3.0漏洞修复
  17. 一个HR工资变化,网友直呼太假了
  18. 阿里规范-为什么不允许魔法值(即未经定义的常量)出现在代码中?
  19. 如何用ArcGIS出一张标准中国地图?常见误区有哪些?
  20. Revit中复合墙图层的规则和CAD识别翻模墙

热门文章

  1. apache日志导入mysql,将Apache访问日志记录到Mysql数据库中
  2. linux可平通网关但不能上网,redhat问题:能ping通网关和本网段的IP,但是不能ping通DNS,也不能上网...
  3. mysql分页查询所有数据库_MySQL 数据库 分页查询/聚合查询
  4. (企业案例)Nacos Config 进阶使用
  5. Element-UI中Cascader 级联选择器使用
  6. (需求实战_进阶_04)SSM集成RabbitMQ 通配符模式 关键代码讲解、开发、测试
  7. 企业实战_14_MyCat跨分片查询_全局表
  8. 使用OSHI 检索系统信息,超牛逼、超级秀、超级巴适!!快来看看吧!不然你会后悔的!!666
  9. .iml文件_jetbrains误删maven 项目.iml文件后的处理方法
  10. java泛型_Java核心知识 基础五 JAVA 泛型