InheritableThreadLocal

初识InheritableThreadLocal

一直来只知道ThreadLocal,直到最近看slf4j MDC实现代码的时候,才认识了InheritableThreadLocal.
InheritableThreadLocal顾名思义,可继承的ThreadLocal.
看类描述:
This class extends  < tt >ThreadLocal </ tt > to provide inheritance of values
 * from parent thread to child thread: when a child thread is created, the
 * child receives initial values for all inheritable thread-local variables
 * for which the parent has values.

测试代码:
 1  public  class Test {
 2 
 3      public  static  void main(String[] args) {
 4          // 使用ThreadLocal,父子线程之间,不共享Value
 5          final ThreadLocal<String> tl =  new ThreadLocal<String>();
 6         tl.set("ThreadLocal-VAL");
 7         System.out.println("Main-1:" + tl.get());
 8          new Thread() {
 9              public  void run() {
10                 System.out.println("Child-1:" + tl.get());
11             };
12         }.start();
13 
14          // 使用InheritableThreadLocal,父线程Value可让子线程共享
15          final ThreadLocal<String> itl =  new InheritableThreadLocal<String>();
16         itl.set("InheritableThreadLocal-VAL");
17         System.out.println("Main-2:" + itl.get());
18          new Thread() {
19              public  void run() {
20                 System.out.println("Child-2:" + itl.get());
21             };
22         }.start();
23 
24     }
25 }

输出内容:
Main-1:ThreadLocal-VAL
Main-2:InheritableThreadLocal-VAL
Child-1:null
Child-2:InheritableThreadLocal-VAL


......分隔符号......

顺带着简单说下MDC.(Mapped Diagnostic Context). 中文直译太恶心了,我理解的意思是,和环境相关的上下文信息.
比如在web应用中,我们可以把用户的ip,访问url等放入到这个上下文中,log打印的时候,就能得到这个信息.

在slf4j BasicMDCAdapter实现中,就是用了InheritableThreadLocal
1  public  class BasicMDCAdapter  implements MDCAdapter {
2 
3    private InheritableThreadLocal inheritableThreadLocal =  new InheritableThreadLocal();
4   
5    // .
6 
7 }


ThreadLocal有个缺陷,在子线程里无法访问父线程的变量, 
InheritableThreadLocal 解决了这个问题,自动会把父线程的变量传递个子线程, 
子线程只能用,修改了不会影响父线程的东西 
这里仍然需要注意并发实现~ 

Java代码   收藏代码
  1. public class Test {  
  2.       
  3.  private final static InheritableThreadLocal<String> holder = new InheritableThreadLocal<String>();  
  4.  public static void main(String[] args){  
  5.      holder.set("aaa");  
  6.      System.out.println("begin=" + holder.get());  
  7.      Thread a = new Thread(){  
  8.         public void run() {  
  9.              System.out.println("thread-begin=" + holder.get());  
  10.              holder.set("vvvvvvvvvvvvv");  
  11.              System.out.println("thread-end=" + holder.get());  
  12.         }  
  13.      };  
  14.   
  15.      a.start();  
  16.      try {  
  17.         Thread.sleep(2000);  
  18.     } catch (InterruptedException e) {  
  19.         e.printStackTrace();  
  20.     }  
  21.      System.out.println("end=" + holder.get());  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值