软件编程
位置:首页>> 软件编程>> java编程>> java多线程编程之InheritableThreadLocal

java多线程编程之InheritableThreadLocal

作者:WAUANG  发布时间:2022-02-08 21:40:32 

标签: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循环一个个的将父线程中的值放入我们新创建的值中。

来源:http://blog.csdn.net/qq_39266910/article/details/78258498

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com