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>() {
@Override
protected 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 功能

ThreadLocal和InheritableThreadLocal使用的更多相关文章

  1. java高并发系列 - 第24天:ThreadLocal、InheritableThreadLocal(通俗易懂)

    java高并发系列第24篇文章. 环境:jdk1.8. 本文内容 需要解决的问题 介绍ThreadLocal 介绍InheritableThreadLocal 需要解决的问题 我们还是以解决问题的方式 ...

  2. ThreadLocal 和 InheritableThreadLocal (引用)

    ThreadLocal:http://www.cnblogs.com/moonandstar08/p/4912673.html InheritableThreadLocal:  http://www. ...

  3. ThreadLocal及InheritableThreadLocal的原理剖析

    我们知道,线程的不安全问题,主要是由于多线程并发读取一个变量而引起的,那么有没有一种办法可以让一个变量是线程独有的呢,这样不就可以解决线程安全问题了么.其实JDK已经为我们提供了ThreadLocal ...

  4. 多线程-ThreadLocal,InheritableThreadLocal

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

  5. 多线程(八)~ThreadLocal、InheritableThreadLocal的使用

    通过前面的学习,我们了解了在多线程+成员变量等情况下会出现线程安全的问题.那么解决线程安全问题除了使用synchronize关键字之外,还有另一种常用的解决思路,那就是使用ThreadLocal类,下 ...

  6. ThreadLocal (二):什么时候使用 InheritableThreadLocal

    一.ThreadLocal 在父子线程传递的问题 public class InheritableThreadLocalDemo { // 全局变量 // static ThreadLocal< ...

  7. 【并发编程】ThreadLocal的兄弟InheritableThreadLocal

    本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 引子 public class InheritableT ...

  8. java并发编程学习: ThreadLocal使用及原理

    多线程应用中,如果希望一个变量隔离在某个线程内,即:该变量只能由某个线程本身可见,其它线程无法访问,那么ThreadLocal可以很方便的帮你做到这一点. 先来看一下示例: package yjmyz ...

  9. InheritableThreadLocal原理

    转载:https://github.com/pzxwhc/MineKnowContainer/issues/20 介绍 InheritableThreadLocal 之前,假设对 ThreadLoca ...

随机推荐

  1. SQL Server 排名函数( ROW_NUMBER、RANK、DENSE_RANK、NTILE )

    排名函数是Sql Server2005新增的功能,下面简单介绍一下他们各自的用法和区别.我们新建一张Order表并添加一些初始数据方便我们查看效果. CREATE TABLE [dbo].[Order ...

  2. JAVA-Servlet高级应用

    会话只是指一段指定的时间间隔. 会话跟踪是维护用户状态(数据)的一种方式.它也被称为servlet中的会话管理. Http协议是一个无状态的,所以我们需要使用会话跟踪技术来维护用户状态. 每次用户请求 ...

  3. Dubbo学习笔记5:Dubbo整体框架分析

    Dubbo的分层架构 本文将简单介绍Dubbo的分层架构设计,如下图是Dubbo官方的整体架构图: Dubbo官方提供的该架构图很复杂,一开始我们没必要深入细节,下面我们简单介绍下其中的主要模块. 其 ...

  4. Spring RedisTemplate操作-发布订阅操作(8)

    @Component("sub") public class Sub implements MessageListener{ @Autowired private StringRe ...

  5. python学习笔记6--双色球需求实现

    # 5,随机产生5条双色球号码 # blue 存蓝色的求 01,02 # red 存红色的求 17,16,03 # date存生成的时间,精确达到秒 #处理 import random,datetim ...

  6. sh脚本学习之:变量

    变量的创建 环境配置 /etc/profile =>~/.bash_profile(~/.bash_login,~/.profile) => ~/.bashrc sh声明 name=&qu ...

  7. ASP.NET实现网站的自动升级

    网站的自动升级主要是要实现从一台服务器上下载某些文件到本服务器上,然后对下载下来的文件进行更新等操作. 比如,现在有服务器A,服务器B和客户端C. 作为COM公司开发的产品DIV网站系统被安装到服务器 ...

  8. 禁止表单操作及JS控制输入的方式

    <div>表单元素特殊属性<input type="text" value="禁止输入" disabled /></div> ...

  9. Linux - sed 常用操作

    sed 文本常用操作方式 sed 10q # 显示文件中的前10行 (模拟"head") sed -n '$=' # 计算行数(模拟 "wc -l") sed ...

  10. 第13月第25天 ios11 uitableview reloaddata contentsize

    1. [tableView reloadData]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_ ...