登录  /  注册
首页 > Java > java教程 > 正文
Java多线程中有关InheritableThreadLocal的详解
黄舟
发布: 2017-10-19 10:00:46
原创
1555人浏览过

这篇文章主要为大家详细介绍了java多线程编程之InheritableThreadLocal,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

InheritableThreadLocal的作用: 当我们需要在子线程中使用父线程中的值得时候我们就可以像使用ThreadLocal那样来使用InheritableThreadLocal了。

首先我们来看一下InheritableThreadLocal的jdk源码:


package java.lang;
import java.lang.ref.*;

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);
  }
}
登录后复制

这段代码就是InheritableThreadLocal的完整源码(删除了很长的注释)。

首先我们可以看到它是继承ThreadLocal类的,然后提供了:

protected T childValue(T parentValue){}方法,这就是InheritableThreadLocal的关键所在,它提供了这个方法,返回父线程中的值,如果还需要在父线程上添加值则可以重写childValue方法。


package InheritableThreadLocal;

import java.util.Date;

public class InheritableThreadLocaExt extends InheritableThreadLocal{
  protected Object initialValue() {
    return new Date().getTime();

  }
  protected Object childValue(Object parentValue) {
    return parentValue+"对继承值进行修改";

  }

}

package InheritableThreadLocal;

public class tool {
  public static InheritableThreadLocaExt t=new InheritableThreadLocaExt();

}

package InheritableThreadLocal;

public class MyThread extends Thread{

  public void run() {
      try {
        for(int i=0;i<10;i++) {
          System.out.println("在线程A中:"+tool.t.get());
        sleep(100);
        }
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }  
}

package InheritableThreadLocal;

public class test {
  public static void main(String[] args) {
    try {
      for(int i=0;i<10;i++) {
        System.out.println("主线程中值:"+tool.t.get());
        Thread.sleep(100);
      }
      Thread.sleep(5000);
      MyThread thread=new MyThread();
      thread.start();

    }catch(InterruptedException e){
      e.printStackTrace();
    }
  }
}
登录后复制

运行输出:

主线程中值:1508210392057
主线程中值:1508210392057
主线程中值:1508210392057
主线程中值:1508210392057
主线程中值:1508210392057
主线程中值:1508210392057
主线程中值:1508210392057
主线程中值:1508210392057
主线程中值:1508210392057
主线程中值:1508210392057
在线程A中:1508210392057对继承值进行修改
在线程A中:1508210392057对继承值进行修改
在线程A中:1508210392057对继承值进行修改
在线程A中:1508210392057对继承值进行修改
在线程A中:1508210392057对继承值进行修改
在线程A中:1508210392057对继承值进行修改
在线程A中:1508210392057对继承值进行修改
在线程A中:1508210392057对继承值进行修改
在线程A中:1508210392057对继承值进行修改
在线程A中:1508210392057对继承值进行修改

是不是有一个疑问,为什么子线程能获取父线程的数据?

我们可以看到InheritableThreadLocal重写了getMap方法和createMap方法,上一节讲ThreadLocal的时候我们知道,ThreadLocal的值是存储在一个叫ThreadLocals的变量中,但是现在返回一个InheritableThreadLocals,这个变量和ThreadLocals是一模一样的只是名字换了,那么究竟 为什么在新的 线程中 通过 threadlocal.get() 方法还能得到值呢?

我们看childValue方法可以猜测到可能在线程创建的时候,做了一些手脚,做了一些值得传递。

我们打开Thread类的源码的时候可以发现 :

ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;

所以当我们创建一个子线程的时候,他就存在一个和ThreadLocals的一样的InheritableThreadLocal变量,再往下看:


private void init(ThreadGroup g, Runnable target, String name,
           long stackSize, AccessControlContext acc,
           .
           .
           if (inheritThreadLocals && parent.inheritableThreadLocals != null)
      this.inheritableThreadLocals =
        ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
登录后复制

重点是以下这段代码:


if (inheritThreadLocals && parent.inheritableThreadLocals != null)
      this.inheritableThreadLocals =
        ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
登录后复制

继续看:


 static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {
    return new ThreadLocalMap(parentMap);
  }
登录后复制


private ThreadLocalMap(ThreadLocalMap parentMap) {
      Entry[] parentTable = parentMap.table;
      int len = parentTable.length;
      setThreshold(len);
      table = new Entry[len];

      for (int j = 0; j < len; j++) {
        Entry e = parentTable[j];
        if (e != null) {
          @SuppressWarnings("unchecked")
          ThreadLocal<Object> key = (ThreadLocal<Object>) e.get();
          if (key != null) {
            Object value = key.childValue(e.value);
            Entry c = new Entry(key, value);
            int h = key.threadLocalHashCode & (len - 1);
            while (table[h] != null)
              h = nextIndex(h, len);
            table[h] = c;
            size++;
          }
        }
      }
    }
登录后复制

有这段代码,先得到父线程(也就是当前执行的线程)的值,然后用for循环一个个的将父线程中的值放入我们新创建的值中。

以上就是Java多线程中有关InheritableThreadLocal的详解的详细内容,更多请关注php中文网其它相关文章!

来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学