1. /*
  2. * @(#)InheritableThreadLocal.java 1.4 02/04/18
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. /**
  9. * This class extends ThreadLocal to provide inheritance of values from parent
  10. * Thread to child Thread: when a child thread is created, the child receives
  11. * initial values for all InheritableThreadLocals for which the parent has
  12. * values. Normally the child's values will be identical to the parent's;
  13. * however, the child's value can be made an arbitrary function of the parent's
  14. * by overriding the childValue method in this class.
  15. * <p>
  16. * InheritableThreadLocal variables are used in preference to ordinary
  17. * ThreadLocal variables when the per-thread-attribute being maintained in the
  18. * variable (e.g., User ID, Transaction ID) must be automatically transmitted
  19. * to any child threads that are created.
  20. *
  21. * @author Josh Bloch and Doug Lea
  22. * @version 1.4 04/18/02
  23. * @see ThreadLocal
  24. * @since JDK1.2
  25. */
  26. public class InheritableThreadLocal extends ThreadLocal {
  27. /**
  28. * Creates an InheritableThreadLocal variable.
  29. */
  30. public InheritableThreadLocal() {
  31. }
  32. /**
  33. * Computes the child's initial value for this InheritableThreadLocal
  34. * as a function of the parent's value at the time the child Thread is
  35. * created. This method is called from within the parent thread before
  36. * the child is started.
  37. * <p>
  38. * This method merely returns its input argument, and should be overridden
  39. * if a different behavior is desired.
  40. */
  41. protected Object childValue(Object parentValue) {
  42. return parentValue;
  43. }
  44. /**
  45. * Get the map associated with a ThreadLocal.
  46. *
  47. * @param t the current thread
  48. */
  49. ThreadLocalMap getMap(Thread t) {
  50. return t.inheritableThreadLocals;
  51. }
  52. /**
  53. * Create the map associated with a ThreadLocal.
  54. *
  55. * @param t the current thread
  56. * @param firstValue value for the initial entry of the table.
  57. * @param map the map to store.
  58. */
  59. void createMap(Thread t, Object firstValue) {
  60. t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);
  61. }
  62. }