实战
直接上测试类和结果:
可以看到当前类可以访问到ThreadLocal和InheritableThreadLocal的数据
新建的子线程可以访问InheritableThreadLocal的数据,而不能访问ThreadLocal的数据
源码:
/**
* @author: 李自强
* @description: lzqtest
* @date: 2021/8/23 5:02 下午
*/
public class lzqtest {
private InheritableThreadLocal<Integer> integerInheritableThreadLocal = new InheritableThreadLocal<>();
private ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
@Test
public void test() throws InterruptedException, ExecutionException {
integerInheritableThreadLocal.set(999);
threadLocal.set(666);
System.out.println("father inherit:" + integerInheritableThreadLocal.get() +
" threadLocal:" + threadLocal.get());
new Thread(() -> System.out.println("son inherit:" + integerInheritableThreadLocal.get() +
" threadLocal:" + threadLocal.get())).start();
}
}
InheritableThreadLocal类结构:
继承了ThreadLocal,get、set方法用的都是父类的方法,只是重写了getMap和createMap:
源码:
ThreadLocalMap getMap(Thread t) {
return t.inheritableThreadLocals;
}
void createMap(Thread t, T firstValue) {
t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);
}
inheritableThreadLocals也是ThreadLocal.ThreadLocalMap对象
我们看一下Thread的构造方法:
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
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();
}
关键代码:
// parent就是父线程,也就是执行new Thread()的线程
if (inheritThreadLocals && parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
这里会把父线程的inheritableThreadLocals赋值给子线程,从而实现了父子线程变量传递