ThreadLocal.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* ThreadLocal -- a variable with a unique value per thread
  2. Copyright (C) 2000, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.lang;
  32. /**
  33. * ThreadLocal objects have a different state associated with every
  34. * Thread that accesses them. Every access to the ThreadLocal object
  35. * (through the <code>get()</code> and <code>set()</code> methods)
  36. * only affects the state of the object as seen by the currently
  37. * executing Thread.
  38. *
  39. * <p>The first time a ThreadLocal object is accessed on a particular
  40. * Thread, the state for that Thread's copy of the local variable is set by
  41. * executing the method <code>initialValue()</code>.
  42. * </p>
  43. *
  44. * <p>An example how you can use this:
  45. * </p>
  46. *
  47. * <pre>
  48. * class Connection
  49. * {
  50. * private static ThreadLocal owner = new ThreadLocal()
  51. * {
  52. * public Object initialValue()
  53. * {
  54. * return("nobody");
  55. * }
  56. * };
  57. * ...
  58. * }
  59. * </pre>
  60. *
  61. * <p>Now all instances of connection can see who the owner of the currently
  62. * executing Thread is by calling <code>owner.get()</code>. By default any
  63. * Thread would be associated with 'nobody'. But the Connection object could
  64. * offer a method that changes the owner associated with the Thread on
  65. * which the method was called by calling <code>owner.put("somebody")</code>.
  66. * (Such an owner changing method should then be guarded by security checks.)
  67. * </p>
  68. *
  69. * <p>When a Thread is garbage collected all references to values of
  70. * the ThreadLocal objects associated with that Thread are removed.
  71. * </p>
  72. *
  73. * @author Mark Wielaard (mark@klomp.org)
  74. * @author Eric Blake (ebb9@email.byu.edu)
  75. * @since 1.2
  76. * @status updated to 1.5
  77. */
  78. public class ThreadLocal<T>
  79. {
  80. /**
  81. * Placeholder to distinguish between uninitialized and null set by the
  82. * user. Do not expose this to the public. Package visible for use by
  83. * InheritableThreadLocal
  84. */
  85. static final Object sentinel = new Object();
  86. /**
  87. * The base for the computation of the next hash for a thread local.
  88. */
  89. private static int nextHashBase = 1;
  90. /**
  91. * Allocate a new hash.
  92. */
  93. private synchronized int computeNextHash()
  94. {
  95. return nextHashBase++ * 6709;
  96. }
  97. /**
  98. * Hash code computed for ThreadLocalMap
  99. */
  100. final int fastHash;
  101. /**
  102. * Creates a ThreadLocal object without associating any value to it yet.
  103. */
  104. public ThreadLocal()
  105. {
  106. fastHash = computeNextHash();
  107. }
  108. /**
  109. * Called once per thread on the first invocation of get(), if set() was
  110. * not already called. The default implementation returns <code>null</code>.
  111. * Often, this method is overridden to create the appropriate initial object
  112. * for the current thread's view of the ThreadLocal.
  113. *
  114. * @return the initial value of the variable in this thread
  115. */
  116. protected T initialValue()
  117. {
  118. return null;
  119. }
  120. /**
  121. * Gets the value associated with the ThreadLocal object for the currently
  122. * executing Thread. If this is the first time the current thread has called
  123. * get(), and it has not already called set(), the value is obtained by
  124. * <code>initialValue()</code>.
  125. *
  126. * @return the value of the variable in this thread
  127. */
  128. public T get()
  129. {
  130. ThreadLocalMap map = Thread.getThreadLocals();
  131. // Note that we don't have to synchronize, as only this thread will
  132. // ever modify the map.
  133. T value = (T) map.get(this);
  134. if (value == sentinel)
  135. {
  136. value = initialValue();
  137. map.set(this, value);
  138. }
  139. return value;
  140. }
  141. /**
  142. * Sets the value associated with the ThreadLocal object for the currently
  143. * executing Thread. This overrides any existing value associated with the
  144. * current Thread and prevents <code>initialValue()</code> from being
  145. * called if this is the first access to this ThreadLocal in this Thread.
  146. *
  147. * @param value the value to set this thread's view of the variable to
  148. */
  149. public void set(T value)
  150. {
  151. ThreadLocalMap map = Thread.getThreadLocals();
  152. // Note that we don't have to synchronize, as only this thread will
  153. // ever modify the map.
  154. map.set(this, value);
  155. }
  156. /**
  157. * Removes the value associated with the ThreadLocal object for the
  158. * currently executing Thread.
  159. * @since 1.5
  160. */
  161. public void remove()
  162. {
  163. ThreadLocalMap map = Thread.getThreadLocals();
  164. map.remove(this);
  165. }
  166. }