前面介绍过ThreadLocal用法,能够经过threadLocal在同一个线程中进行值传递,可是在父子线程中就不能进行值传递了,由于不是同一个线程,因此对应的ThreadLocalMap是不同的bash
ThreadLocal示例app
public class ThreadLocalTest {
public static ThreadLocal<String> threadLocal = new ThreadLocal<>();
public static String get() {
return threadLocal.get();
}
public static void set(String value) {
threadLocal.set(value);
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
final int j = i;
ThreadLocalTest.set("ye");
Thread t = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":" + ThreadLocalTest.get());
}
});
t.start();
}
}
}
复制代码
结果: less
public class InheritableThreadLocalTest {
public static ThreadLocal<String> threadLocal = new InheritableThreadLocal<>();
public static String get() {
return threadLocal.get();
}
public static void set(String value) {
threadLocal.set(value);
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
InheritableThreadLocalTest.set("ye");
Thread t = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":" + InheritableThreadLocalTest.get());
}
});
t.start();
}
}
}
复制代码
结果: ide
InheritableThreadLocal是继承ThreadLocal 先看他set方法,set 方法是调用 ThreadLocal的set方法
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
复制代码
createMap调用本身重写的函数
void createMap(Thread t, T firstValue) {
t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);
}
复制代码
看上面发现和ThreadLocal的createMap差很少,初始化一个ThreadLocalMap,只是赋值给了inheritableThreadLocals,而ThreadLocal赋值给了threadLocalspost
继续看get方法,get方法也是调用ThreadLocal的get方法ui
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
复制代码
区别在于getMap调用的是InheritableThreadLocal重写的方法this
ThreadLocalMap getMap(Thread t) {
return t.inheritableThreadLocals;
}
复制代码
由于在set的时候赋值给的是t.inheritableThreadLocals,因此取map的时候也是从t.inheritableThreadLocals取的spa
看完了set和get方法,惟一的区别就是map存取的地方不同,可却没有看出来其余不一样,只是换了一个变量而已,那他是怎么和ThreadLocal 不同,是怎么完成父子线程的值传递呢 这就要看Thread的初始化了,看Thread类的构造方法你就会看到都会调用一个init方法线程
Thread的init方法
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals) {
if (name == null) {
throw new NullPointerException("name cannot be null");
}
this.name = name;
Thread parent = currentThread();
SecurityManager security = System.getSecurityManager();
if (g == null) {
/* Determine if it's an applet or not */ /* If there is a security manager, ask the security manager what to do. */ if (security != null) { g = security.getThreadGroup(); } /* If the security doesn't have a strong opinion of the matter
use the parent thread group. */
if (g == null) {
g = parent.getThreadGroup();
}
}
/* checkAccess regardless of whether or not threadgroup is
explicitly passed in. */
g.checkAccess();
/*
* Do we have the required permissions?
*/
if (security != null) {
if (isCCLOverridden(getClass())) {
security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
}
}
g.addUnstarted();
this.group = g;
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
if (security == null || isCCLOverridden(parent.getClass()))
this.contextClassLoader = parent.getContextClassLoader();
else
this.contextClassLoader = parent.contextClassLoader;
this.inheritedAccessControlContext =
acc != null ? acc : AccessController.getContext();
this.target = target;
setPriority(priority);
if (inheritThreadLocals && parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
/* Stash the specified stack size in case the VM cares */
this.stackSize = stackSize;
/* Set thread ID */
tid = nextThreadID();
}
复制代码
上面的方法主要看
if (inheritThreadLocals && parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
复制代码
inheritThreadLocals 是方法的入参,看构造函数只有Thread(Runnable target, AccessControlContext acc)是false,其他的构造方法都是true,因此咱们这里传的是true
Thread(Runnable target, AccessControlContext acc) {
init(null, target, "Thread-" + nextThreadNum(), 0, acc, false);
}
复制代码
Thread parent = currentThread(); 这里指的是咱们的main线程,由于咱们在main里面用的是InheritableThreadLocalTest.set("ye"); 因此咱们把ThreadLocalMap赋值给了inheritableThreadLocals 综上所述会走this.inheritableThreadLocals = ThreadLocal.createInheritedMap(parent.inheritableThreadLocals); this 为当前new的子线程
接下来就要看子线程的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++;
}
}
}
}
复制代码
上述代码很简单,就是拿到父线程的ThreadLocalMap,而后进行复制(浅拷贝,引用复制),这样子线程的inheritableThreadLocals就有了对应的ThreadLocalMap,这样经过ThreadLocalMap就能够取到和父线程一样的值了
InheritableThreadLocal 继承ThreadLocal ,因此用法和ThreadLocal 同样, 惟一不一样的是ThreadLocal用的是ThreadLocal 用的是ThreadLocal.ThreadLocalMap threadLocals变量 InheritableThreadLocal用的是ThreadLocal.ThreadLocalMap inheritableThreadLocals变量 但都是ThreadLocalMap,因此get和set本质上是没有区别的 InheritableThreadLocal之因此能够支持父子线程直接的传递 是在new Thread的时候init中 复制父线程的ThreadLocalMap 到子线程的inheritableThreadLocals中