ThreadLocal与InheritableThreadLocal

threadlocal持有的对象不能被子线程访问到。

InheritableThreadlLocal持有的对象,在子线程中会有相同的引用,也就是说子线程会复制父线程中threadlocal的引用。

private static final ThreadLocal<Map<Object, Object>> resources = new ThreadContext.InheritableThreadLocalMap();

像上面这种应用,就可以在父线程和子线程中共享变量。
这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/ljz2016/article/details/80838696