libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / lang / ThreadLocal.java
blob14778c65c2fffe96a86d3b1acbe7047753c9866c
1 /* ThreadLocal -- a variable with a unique value per thread
2 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package java.lang;
40 /**
41 * ThreadLocal objects have a different state associated with every
42 * Thread that accesses them. Every access to the ThreadLocal object
43 * (through the <code>get()</code> and <code>set()</code> methods)
44 * only affects the state of the object as seen by the currently
45 * executing Thread.
47 * <p>The first time a ThreadLocal object is accessed on a particular
48 * Thread, the state for that Thread's copy of the local variable is set by
49 * executing the method <code>initialValue()</code>.
50 * </p>
52 * <p>An example how you can use this:
53 * </p>
55 * <pre>
56 * class Connection
57 * {
58 * private static ThreadLocal owner = new ThreadLocal()
59 * {
60 * public Object initialValue()
61 * {
62 * return("nobody");
63 * }
64 * };
65 * ...
66 * }
67 * </pre>
69 * <p>Now all instances of connection can see who the owner of the currently
70 * executing Thread is by calling <code>owner.get()</code>. By default any
71 * Thread would be associated with 'nobody'. But the Connection object could
72 * offer a method that changes the owner associated with the Thread on
73 * which the method was called by calling <code>owner.put("somebody")</code>.
74 * (Such an owner changing method should then be guarded by security checks.)
75 * </p>
77 * <p>When a Thread is garbage collected all references to values of
78 * the ThreadLocal objects associated with that Thread are removed.
79 * </p>
81 * @author Mark Wielaard (mark@klomp.org)
82 * @author Eric Blake (ebb9@email.byu.edu)
83 * @since 1.2
84 * @status updated to 1.5
86 public class ThreadLocal<T>
88 /**
89 * Placeholder to distinguish between uninitialized and null set by the
90 * user. Do not expose this to the public. Package visible for use by
91 * InheritableThreadLocal
93 static final Object notFound = new Object();
95 /**
96 * The base for the computation of the next hash for a thread local.
98 private static int nextHashBase = 1;
101 * Allocate a new hash.
103 private synchronized int computeNextHash() {
104 return nextHashBase++ * 6709;
108 * Hash code computed for ThreadLocalMap
110 final int fastHash;
113 * Creates a ThreadLocal object without associating any value to it yet.
115 public ThreadLocal()
117 fastHash = computeNextHash();
121 * Called once per thread on the first invocation of get(), if set() was
122 * not already called. The default implementation returns <code>null</code>.
123 * Often, this method is overridden to create the appropriate initial object
124 * for the current thread's view of the ThreadLocal.
126 * @return the initial value of the variable in this thread
128 protected T initialValue()
130 return null;
134 * Gets the value associated with the ThreadLocal object for the currently
135 * executing Thread. If this is the first time the current thread has called
136 * get(), and it has not already called set(), the value is obtained by
137 * <code>initialValue()</code>.
139 * @return the value of the variable in this thread
141 public T get()
143 ThreadLocalMap map = Thread.getThreadLocals();
144 // Note that we don't have to synchronize, as only this thread will
145 // ever modify the map.
146 T value = (T) map.get(this);
147 if (value == notFound)
149 value = initialValue();
150 map.set(this, value);
152 return value;
156 * Sets the value associated with the ThreadLocal object for the currently
157 * executing Thread. This overrides any existing value associated with the
158 * current Thread and prevents <code>initialValue()</code> from being
159 * called if this is the first access to this ThreadLocal in this Thread.
161 * @param value the value to set this thread's view of the variable to
163 public void set(T value)
165 ThreadLocalMap map = Thread.getThreadLocals();
166 // Note that we don't have to synchronize, as only this thread will
167 // ever modify the map.
168 map.set(this, value);
172 * Removes the value associated with the ThreadLocal object for the
173 * currently executing Thread.
174 * @since 1.5
176 public void remove()
178 ThreadLocalMap map = Thread.getThreadLocals();
179 map.remove(this);