ThreadLocal和InheritableThreadLocal使用
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使用的更多相关文章
- java高并发系列 - 第24天:ThreadLocal、InheritableThreadLocal(通俗易懂)
java高并发系列第24篇文章. 环境:jdk1.8. 本文内容 需要解决的问题 介绍ThreadLocal 介绍InheritableThreadLocal 需要解决的问题 我们还是以解决问题的方式 ...
- ThreadLocal 和 InheritableThreadLocal (引用)
ThreadLocal:http://www.cnblogs.com/moonandstar08/p/4912673.html InheritableThreadLocal: http://www. ...
- ThreadLocal及InheritableThreadLocal的原理剖析
我们知道,线程的不安全问题,主要是由于多线程并发读取一个变量而引起的,那么有没有一种办法可以让一个变量是线程独有的呢,这样不就可以解决线程安全问题了么.其实JDK已经为我们提供了ThreadLocal ...
- 多线程-ThreadLocal,InheritableThreadLocal
ThreadLocal 变量值得共享可以使用public static变量的形式,所有的线程都使用同一个public static变量.如果想实现每一个线程都有自己的共享变量该如何解决呢?JDK中提供 ...
- 多线程(八)~ThreadLocal、InheritableThreadLocal的使用
通过前面的学习,我们了解了在多线程+成员变量等情况下会出现线程安全的问题.那么解决线程安全问题除了使用synchronize关键字之外,还有另一种常用的解决思路,那就是使用ThreadLocal类,下 ...
- ThreadLocal (二):什么时候使用 InheritableThreadLocal
一.ThreadLocal 在父子线程传递的问题 public class InheritableThreadLocalDemo { // 全局变量 // static ThreadLocal< ...
- 【并发编程】ThreadLocal的兄弟InheritableThreadLocal
本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 引子 public class InheritableT ...
- java并发编程学习: ThreadLocal使用及原理
多线程应用中,如果希望一个变量隔离在某个线程内,即:该变量只能由某个线程本身可见,其它线程无法访问,那么ThreadLocal可以很方便的帮你做到这一点. 先来看一下示例: package yjmyz ...
- InheritableThreadLocal原理
转载:https://github.com/pzxwhc/MineKnowContainer/issues/20 介绍 InheritableThreadLocal 之前,假设对 ThreadLoca ...
随机推荐
- (转)Maven学习总结(六)——Maven与Eclipse整合
孤傲苍狼只为成功找方法,不为失败找借口! Maven学习总结(六)——Maven与Eclipse整合 一.安装Maven插件 下载下来的maven插件如下图所示:,插件存放的路径是:E:/MavenP ...
- redis 中用正则找key
获取 redis 中所有的 key 可用使用 *. redis 127.0.0.1:6379> KEYS * 1) "w3c3" 2) "w3c1" 3) ...
- RestFul风格接口示例
REST是英文representational state transfer(表象性状态转变)或者表述性状态转移;Rest是web服务的一种架构风格;使用HTTP,URI,XML,JSON,HTML等 ...
- bzoj千题计划233:bzoj 1304: [CQOI2009]叶子的染色
http://www.lydsy.com/JudgeOnline/problem.php?id=1304 结论1:根节点一定染色 如果根节点没有染色,选择其子节点的一个颜色,那么所有这个颜色的子节点都 ...
- 使用 maven 搭建web开发基本架构
我觉得可能,对于还没有使用过 IDEA 进行开发的童鞋来说,直接撸代码是有些尴尬的.那么我会把示例代码之前的那些事再在这里写一遍 按图步骤进行即可进行基本项目结构搭建 现在基本流行 maven 管理项 ...
- chrome 隐藏技能之 base64 图片转换
有时候我们要转换图片为base64,或者将base64转回图片,可能都需要找一些在线工具或者软件类型的工具才行.当然 chrome 也算是软件,但是好在做前端的都有 chrome.好了,来看下简单的例 ...
- 通过vnc访问无显卡服务器的图形环境
最近在一台没有显卡的 Power 服务器上,安装了Fedora 22,因为没有显卡,所以不能在本机启动Xserver,于是想通过vnc的方式远程访问服务器的图形环境. 在服务器上安装好xserver和 ...
- mysql.user细节三问
一.如何拒绝用户从某个精确ip访问数据库假如在mysql.user表中存在用户'mydba'@'192.168.85.%',现在想拒绝此用户从某个精确ip访问数据库 # 创建精确ip用户,分配不同的密 ...
- JS判断客户浏览器是否是IE8浏览器、jQuery判断浏览器内核
今天在使用encharts的时候由于要兼容IE8,所以最终决定在非IE8浏览器使用encharts,在IE8使用amcharts.于是需要使用JS判断使用的浏览器版本: function IEVers ...
- 转:存储之直连存储Dell Powervault MD 3000
存储之直连存储DellPowervault MD 3000 存储根据服务器类型可以分为:封闭系统的存储和开放系统的存储 1.封闭系统的存储:封闭系统主要指大型机,AS400等服务器 2.开放系统的存储 ...