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. php错误日志

    php错误日志 /usr/local/php/var/log/php-fpm.log」—————————

  2. 【CSS】float属性

    float浮动属性1.作用: 将页面元素浮动起来,使其能够向左或者向右排列 2.应用: 实现页面中布局的左右排版 实现图文环绕的版式效果 3.值: 4.原理: 浮动元素将脱离默认的文档流,漂浮在默认文 ...

  3. Oracle DBMS_RANDOM

    DBMS_RANDOM.VALUE -- [0,1)的38位精度小数 SELECT DBMS_RANDOM.VALUE FROM DUAL; -- [10,20)的38位精度小数 SELECT DBM ...

  4. Java基础-Java中的内存分配与回收机制

    Java基础-Java中的内存分配与回收机制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一. 二.

  5. Linux文本处理工具——Sed

    sed:数据流编辑器: awk:报告文本的生成器 sed 基本用法:(Stream EDitor) Stream 流 EDitor 编辑器 行编辑器 全屏编辑器:vi/vimsed:内存空间(模式空间 ...

  6. HDU 1524 树上无环博弈 暴力SG

    一个拓扑结构的图,给定n个棋的位置,每次可以沿边走,不能操作者输. 已经给出了拓扑图了,对于每个棋子找一遍SG最后SG和就行了. /** @Date : 2017-10-13 20:08:45 * @ ...

  7. p 最多两行 多的显示省略号

    -webkit-line-clamp: 2 -webkit-box-orient: vertical; }

  8. Spark笔记之Catalog

    一.什么是Catalog Spark SQL提供了执行sql语句的支持,sql语句是以表的方式组织使用数据的,而表本身是如何组织存储的呢,肯定是存在一些元数据之类的东西了,Catalog就是Spark ...

  9. __class__属性与元类

    class M(type): def __str__(self): return "gege" aa = "ccf" cc = "ccc" ...

  10. Ubuntu接显示器问题

    1.Could not apply the stored configuration for monitors 解决办法:Ubuntu在开机进入桌面的时候,会调用gnome-setting-deamo ...