001/* ThreadLocal -- a variable with a unique value per thread 002 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038package java.lang; 039 040/** 041 * ThreadLocal objects have a different state associated with every 042 * Thread that accesses them. Every access to the ThreadLocal object 043 * (through the <code>get()</code> and <code>set()</code> methods) 044 * only affects the state of the object as seen by the currently 045 * executing Thread. 046 * 047 * <p>The first time a ThreadLocal object is accessed on a particular 048 * Thread, the state for that Thread's copy of the local variable is set by 049 * executing the method <code>initialValue()</code>. 050 * </p> 051 * 052 * <p>An example how you can use this: 053 * </p> 054 * 055 * <pre> 056 * class Connection 057 * { 058 * private static ThreadLocal owner = new ThreadLocal() 059 * { 060 * public Object initialValue() 061 * { 062 * return("nobody"); 063 * } 064 * }; 065 * ... 066 * } 067 * </pre> 068 * 069 * <p>Now all instances of connection can see who the owner of the currently 070 * executing Thread is by calling <code>owner.get()</code>. By default any 071 * Thread would be associated with 'nobody'. But the Connection object could 072 * offer a method that changes the owner associated with the Thread on 073 * which the method was called by calling <code>owner.put("somebody")</code>. 074 * (Such an owner changing method should then be guarded by security checks.) 075 * </p> 076 * 077 * <p>When a Thread is garbage collected all references to values of 078 * the ThreadLocal objects associated with that Thread are removed. 079 * </p> 080 * 081 * @author Mark Wielaard (mark@klomp.org) 082 * @author Eric Blake (ebb9@email.byu.edu) 083 * @since 1.2 084 * @status updated to 1.5 085 */ 086public class ThreadLocal<T> 087{ 088 /** 089 * Placeholder to distinguish between uninitialized and null set by the 090 * user. Do not expose this to the public. Package visible for use by 091 * InheritableThreadLocal 092 */ 093 static final Object sentinel = new Object(); 094 095 /** 096 * The base for the computation of the next hash for a thread local. 097 */ 098 private static int nextHashBase = 1; 099 100 /** 101 * Allocate a new hash. 102 */ 103 private synchronized int computeNextHash() 104 { 105 return nextHashBase++ * 6709; 106 } 107 108 /** 109 * Hash code computed for ThreadLocalMap 110 */ 111 final int fastHash; 112 113 /** 114 * Creates a ThreadLocal object without associating any value to it yet. 115 */ 116 public ThreadLocal() 117 { 118 fastHash = computeNextHash(); 119 } 120 121 /** 122 * Called once per thread on the first invocation of get(), if set() was 123 * not already called. The default implementation returns <code>null</code>. 124 * Often, this method is overridden to create the appropriate initial object 125 * for the current thread's view of the ThreadLocal. 126 * 127 * @return the initial value of the variable in this thread 128 */ 129 protected T initialValue() 130 { 131 return null; 132 } 133 134 /** 135 * Gets the value associated with the ThreadLocal object for the currently 136 * executing Thread. If this is the first time the current thread has called 137 * get(), and it has not already called set(), the value is obtained by 138 * <code>initialValue()</code>. 139 * 140 * @return the value of the variable in this thread 141 */ 142 public T get() 143 { 144 ThreadLocalMap map = Thread.getThreadLocals(); 145 // Note that we don't have to synchronize, as only this thread will 146 // ever modify the map. 147 T value = (T) map.get(this); 148 if (value == sentinel) 149 { 150 value = initialValue(); 151 map.set(this, value); 152 } 153 return value; 154 } 155 156 /** 157 * Sets the value associated with the ThreadLocal object for the currently 158 * executing Thread. This overrides any existing value associated with the 159 * current Thread and prevents <code>initialValue()</code> from being 160 * called if this is the first access to this ThreadLocal in this Thread. 161 * 162 * @param value the value to set this thread's view of the variable to 163 */ 164 public void set(T value) 165 { 166 ThreadLocalMap map = Thread.getThreadLocals(); 167 // Note that we don't have to synchronize, as only this thread will 168 // ever modify the map. 169 map.set(this, value); 170 } 171 172 /** 173 * Removes the value associated with the ThreadLocal object for the 174 * currently executing Thread. 175 * @since 1.5 176 */ 177 public void remove() 178 { 179 ThreadLocalMap map = Thread.getThreadLocals(); 180 map.remove(this); 181 } 182}