深入理解ThreadLocal(二)

时间:2023-03-09 04:27:43
深入理解ThreadLocal(二)

3 InheritableThreadLocal的使用

通过上面的分析知道通过ThreadLocal保存的值是线程隔离的。其实在Thread对象中,还有一个ThreadLocal.ThreadLocalMap类型的成员变量inheritableThreadLocals。inheritableThreadLocals其实是一个InheritableThreadLocals类型,InheritableThreadLocals是ThreadLocal的子类。保存在inheritableThreadLocals中的值可以传递给子线程。下面看一个例子:

[java] view plain copy

  1. public class InheritableThreadLocalTest {

  2. static InheritableThreadLocal<Long> mInheritableThreadLocal = new InheritableThreadLocal<Long>();

  3. static ThreadLocal<Long> mThreadLocal = new ThreadLocal<Long>();

  4. static SimpleInheritableThreadLocal<Long> mSimpleInheritableThreadLocal=new SimpleInheritableThreadLocal<Long>();

  5. public static void printValue() {

  6. System.out.println("Thread " + Thread.currentThread().getId() + ":\t valueFromParent=" + mInheritableThreadLocal.get() + "\t valueFromLocal="

  7. + mThreadLocal.get() +"\tsimpleValueFromParent="+mSimpleInheritableThreadLocal.get());

  8. }

  9. static class PrintValueRunnable implements Runnable {

  10. @Override

  11. public void run() {

  12. // TODO Auto-generated method stub

  13. InheritableThreadLocalTest.printValue();

  14. if (Thread.currentThread().getId() % 2 == 0) {

  15. mInheritableThreadLocal.set(mInheritableThreadLocal.get()+1);

  16. }

  17. InheritableThreadLocalTest.printValue();

  18. }

  19. }

  20. public static void main(String[] args) {

  21. long tid = Thread.currentThread().getId();

  22. mInheritableThreadLocal.set(tid);

  23. mThreadLocal.set(tid);

  24. mSimpleInheritableThreadLocal.set(tid);

  25. System.out.println("mainThread: " + "\t valueFromLocal=" + mThreadLocal.get());

  26. new Thread(new PrintValueRunnable()).start();

  27. new Thread(new PrintValueRunnable()).start();

  28. new Thread(new PrintValueRunnable()).start();

  29. }

  30. }

打印结果如下:

深入理解ThreadLocal(二)

在这个例子,先在主线程中new一个InheritableThreadLocal对象,然后在子线程中访问。通过打印结果可以看到,主线程设置到InheritableThreadLocal中的值会复制给子线程,然后父子线程可以各自独立读写保存在InheritableThreadLocal中的值。可以还有人注意到了,在上面的例子中还有一个SimpleInheritableThreadLocal对象,这个是我自定义,它继承了InheritableThreadLocal,并重写了InheritableThreadLocal.childValue。我们可以通过重写childValue来控制从父线程中继承过来的value的初始值。在SimpleInheritableThreadLocal中会对从父线程继承的Long对象做加1处理。默认情况下,是不做任何处理的。SimpleInheritableThreadLocal的代码如下:

[java] view plain copy

  1. public class SimpleInheritableThreadLocal<T> extends InheritableThreadLocal<T> {

  2. @Override

  3. protected T childValue(T parentValue) {

  4. // TODO Auto-generated method stub

  5. if (parentValue instanceof Long) {

  6. Long res = (Long) parentValue + 1;

  7. return (T) res;

  8. }

  9. return super.childValue(parentValue);

  10. }

  11. }

可能有人会有疑问:ThreadLocal是线程隔离的,而InheritableThreadLocal又是ThreadLocal的子类,所以InheritableThreadLocal也应该是线程隔离的。那这里是怎么做到从父线程继承值呢?子线想一想:实现线程隔离的核心思想是将要隔离的变量保存到Thread对象里面,然后通过ThreadLocal来读写这个变量;其实不通过ThreadLocal,在Thread内部是可以直接读写threadLocals的。那既然inheritableThreadLocals是Thread的成员变量,那么在初始化Thread的时候就可以直接读取父线程的inheritableThreadLocals并把它保存的values设置为子线程的inheritableThreadLocals初始值。

这个想法对不对呢?我们来看一下Thread初始化部分的源码:

[java] view plain copy

  1. public Thread(Runnable target) {

  2. init(null, target, "Thread-" + nextThreadNum(), 0);

  3. }

  4. private void init(ThreadGroup g, Runnable target, String name,

  5. long stackSize) {

  6. init(g, target, name, stackSize, null);

  7. }

  8. private void init(ThreadGroup g, Runnable target, String name,

  9. long stackSize, AccessControlContext acc) {

  10. if (name == null) {

  11. throw new NullPointerException("name cannot be null");

  12. }

  13. this.name = name.toCharArray();

  14. Thread parent = currentThread();

  15. SecurityManager security = System.getSecurityManager();

  16. if (g == null) {

  17. /* Determine if it's an applet or not */

  18. /* If there is a security manager, ask the security manager

  19. what to do. */

  20. if (security != null) {

  21. g = security.getThreadGroup();

  22. }

  23. /* If the security doesn't have a strong opinion of the matter

  24. use the parent thread group. */

  25. if (g == null) {

  26. g = parent.getThreadGroup();

  27. }

  28. }

  29. /* checkAccess regardless of whether or not threadgroup is

  30. explicitly passed in. */

  31. g.checkAccess();

  32. /*

  33. * Do we have the required permissions?

  34. */

  35. if (security != null) {

  36. if (isCCLOverridden(getClass())) {

  37. security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);

  38. }

  39. }

  40. g.addUnstarted();

  41. this.group = g;

  42. this.daemon = parent.isDaemon();

  43. this.priority = parent.getPriority();

  44. if (security == null || isCCLOverridden(parent.getClass()))

  45. this.contextClassLoader = parent.getContextClassLoader();

  46. else

  47. this.contextClassLoader = parent.contextClassLoader;

  48. this.inheritedAccessControlContext =

  49. acc != null ? acc : AccessController.getContext();

  50. this.target = target;

  51. setPriority(priority);

  52. if (parent.inheritableThreadLocals != null)

  53. this.inheritableThreadLocals =

  54. ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);

  55. /* Stash the specified stack size in case the VM cares */

  56. this.stackSize = stackSize;

  57. /* Set thread ID */

  58. tid = nextThreadID();

  59. }

先看Thread的构造函数,这里调用了init函数,最终会调用这init(ThreadGroup g, Runnable target, String name,long stackSize, AccessControlContext acc)函数。直接看第60行,如果父线程的parent.inheritableThreadLocals不为null,就会以父线程的inheritableThreadLocals为参数来构造子线程的inheritableThreadLocals。我们再看一下createInheritedMap函数:

[java] view plain copy

  1. static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {

  2. return new ThreadLocalMap(parentMap);

  3. }

  4. private ThreadLocalMap(ThreadLocalMap parentMap) {

  5. Entry[] parentTable = parentMap.table;

  6. int len = parentTable.length;

  7. setThreshold(len);

  8. table = new Entry[len];

  9. for (int j = 0; j < len; j++) {

  10. Entry e = parentTable[j];

  11. if (e != null) {

  12. ThreadLocal key = e.get();

  13. if (key != null) {

  14. Object value = key.childValue(e.value);

  15. Entry c = new Entry(key, value);

  16. int h = key.threadLocalHashCode & (len - 1);

  17. while (table[h] != null)

  18. h = nextIndex(h, len);

  19. table[h] = c;

  20. size++;

  21. }

  22. }

  23. }

  24. }

到这里就可以看到:在ThreadLocalMap的构造函数中会遍历保存父线程的inheritableThreadLocals中键值对,并且在读取value的会调用key.childValue计算子线程中的value。所以在SimpleInheritableThreadLocal重写了childValue来根据父线程的value计算子线程的value,默认情况是不做任何处理,直接返回的,如:

[java] view plain copy

  1. T childValue(T parentValue) {

  2. throw new UnsupportedOperationException();

  3. }