Source for java.lang.ThreadLocal

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