| 1 | /* java.lang.ThreadLocal
|
|---|
| 2 | Copyright (C) 2000 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., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 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 | import java.util.WeakHashMap;
|
|---|
| 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 | * <p>
|
|---|
| 50 | * The first time a ThreadLocal object is accessed on a particular
|
|---|
| 51 | * Thread (and no state is associated with that Thread yet)
|
|---|
| 52 | * the state for that Thread is set by executing the method
|
|---|
| 53 | * <code>initialValue()</code>.
|
|---|
| 54 | * <p>
|
|---|
| 55 | * An example how you can use this:
|
|---|
| 56 | * <pre>
|
|---|
| 57 | * class Connection {
|
|---|
| 58 | * private static ThreadLocal owner = new ThreadLocal() {
|
|---|
| 59 | * public Object initialValue() {
|
|---|
| 60 | * return("nobody");
|
|---|
| 61 | * }
|
|---|
| 62 | * };
|
|---|
| 63 | * ...
|
|---|
| 64 | * }
|
|---|
| 65 | * </pre>
|
|---|
| 66 | * Now all instances of connection can see who the owner of the currently
|
|---|
| 67 | * executing Thread is by calling <code>owner.get()</code>. By default any
|
|---|
| 68 | * Thread would be associated with 'nobody'. But the Connection object could
|
|---|
| 69 | * offer a method that changes the owner associated with the Thread on
|
|---|
| 70 | * which the method was called by calling <code>owner.put("somebody")</code>.
|
|---|
| 71 | * (Such an owner changing method should then be guarded by security checks.)
|
|---|
| 72 | * <p>
|
|---|
| 73 | * When a Thread is garbage collected all references to values of
|
|---|
| 74 | * the ThreadLocal objects associated with that Thread are removed.
|
|---|
| 75 | *
|
|---|
| 76 | * @since 1.2
|
|---|
| 77 | * @author Mark Wielaard ([email protected])
|
|---|
| 78 | */
|
|---|
| 79 | public class ThreadLocal {
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Trivial container to wrap the stored values.
|
|---|
| 83 | * Needed to see if the value is null or not yet set.
|
|---|
| 84 | * If it is not yet set we must call intialValue() once.
|
|---|
| 85 | * Package local so InheritableThreadLocal can see it.
|
|---|
| 86 | */
|
|---|
| 87 | final static class Value {
|
|---|
| 88 | final Object value;
|
|---|
| 89 |
|
|---|
| 90 | Value(Object value) {
|
|---|
| 91 | this.value = value;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | Object getValue() {
|
|---|
| 95 | return value;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Maps Threads to Values. Uses a WeakHashMap so if a Thread is garbage
|
|---|
| 101 | * collected the reference to the Value will disappear. Only the
|
|---|
| 102 | * <code>set(Thread, Value)</code> and <code>get(Thread)</code> methods
|
|---|
| 103 | * access it. Since this can happen from multiple Threads simultaniously
|
|---|
| 104 | * those methods are synchronized.
|
|---|
| 105 | */
|
|---|
| 106 | private final Map valueMap = new WeakHashMap();
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * Creates a ThreadLocal object without associating any value to it
|
|---|
| 110 | * yet.
|
|---|
| 111 | */
|
|---|
| 112 | public ThreadLocal() {
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Gets the value associated with the ThreadLocal object for the
|
|---|
| 117 | * currently executing Thread. If there is no value is associated
|
|---|
| 118 | * with this Thread yet then the valued returned by the
|
|---|
| 119 | * <code>initialValue()</code> method is assosiated with this Thread
|
|---|
| 120 | * and returned.
|
|---|
| 121 | */
|
|---|
| 122 | public Object get() {
|
|---|
| 123 | Thread currentThread = Thread.currentThread();
|
|---|
| 124 | Value v = get(currentThread);
|
|---|
| 125 | if (v == null) {
|
|---|
| 126 | v = new Value(initialValue());
|
|---|
| 127 | set(currentThread, v);
|
|---|
| 128 | }
|
|---|
| 129 | return v.getValue();
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /**
|
|---|
| 133 | * Gets the Value of this ThreadLocal for a particular Thread.
|
|---|
| 134 | * It is synchronized so the <code>set(Thread, Value)</code> method cannot
|
|---|
| 135 | * simultaniously modify the </code>valueMap</code> from another thread.
|
|---|
| 136 | * Package local so InheritableThreadLocal can access it when a new child
|
|---|
| 137 | * Thread inherits values from its parent Thread.
|
|---|
| 138 | */
|
|---|
| 139 | synchronized final Value get(Thread thread) {
|
|---|
| 140 | return (Value)valueMap.get(thread);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /**
|
|---|
| 144 | * Sets the value associated with the ThreadLocal object for the
|
|---|
| 145 | * currently executing Thread. This overrides any existing value
|
|---|
| 146 | * associated with the current Thread and does not call the
|
|---|
| 147 | * <code>initialValue()</code> method, even if this is the first
|
|---|
| 148 | * time this Thread accesses this ThreadLocal.
|
|---|
| 149 | */
|
|---|
| 150 | public void set(Object value) {
|
|---|
| 151 | Thread currentThread = Thread.currentThread();
|
|---|
| 152 | Value v = new Value(value);
|
|---|
| 153 | set(currentThread, v);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /**
|
|---|
| 157 | * Sets the Value for this ThreadLocal for a particular Thread.
|
|---|
| 158 | * It is synchronized so the <code>get(Thread)</code> method cannot
|
|---|
| 159 | * simultaniously read the </code>valueMap</code> from another thread.
|
|---|
| 160 | * Package local so InheritableThreadLocal can access it when a new child
|
|---|
| 161 | * Thread inherits values from its parent Thread.
|
|---|
| 162 | */
|
|---|
| 163 | synchronized final void set(Thread thread, Value value) {
|
|---|
| 164 | valueMap.put(thread, value);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /**
|
|---|
| 168 | * Called when <code>get()</code> is called and no state is associated
|
|---|
| 169 | * with the currently executing Thread yet.
|
|---|
| 170 | * <p>
|
|---|
| 171 | * The default implementation returns <code>null</code>.
|
|---|
| 172 | */
|
|---|
| 173 | protected Object initialValue() {
|
|---|
| 174 | return null;
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|