一、java子线程中获取父线程的threadLocal中的值
threadLocal可以做线程级的数据隔离,那如何在子线程中获取父线程的值呢? 可以使用InheritableThreadLocal
/**
* Java子线程获取父线程的threadLocal中的值
*/
public class ThreadDemo1 {
public static final InheritableThreadLocal inheritableThreadLocal = new InheritableThreadLocal<>();
public static final ThreadLocal threadLocal = new ThreadLocal<>();
public static void main(String[] args) throws Exception {
inheritableThreadLocal.set("inheritableThreadLocal hello");
threadLocal.set("threadLocal world");
new Thread(()->{
System.out.println(String.format("子线程可继承值:%s", inheritableThreadLocal.get()));
System.out.println(String.format("子线程值:%s", threadLocal.get()));
new Thread(()->{
System.out.println(String.format("孙线程可继承值&#x