home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 3.4 KB | 109 lines |
- /*
- * @(#)ThreadLocal.java 1.3 98/03/18
- *
- * Copyright 1997 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.lang;
- import java.util.Hashtable;
-
- /**
- * This class provides ThreadLocal variables. These variables differ from
- * their normal counterparts in that each thread that accesses one (via its
- * get or set method) has its own, independently initialized copy of the
- * variable. ThreadLocal objects are typically private static variables in
- * classes that wish to associate state with a thread (e.g., a user ID or
- * Transaction ID).
- *
- * Each thread holds an implicit.reference to its copy of a ThreadLocal
- * as long as the thread is alive and the ThreadLocal object is accessible;
- * after a thread goes away, all of its copies of ThreadLocal variables are
- * subject to garbage collection (unless other references to these copies
- * exist).
- *
- * @author Josh Bloch
- * @version 1.3 03/18/98
- */
-
- public class ThreadLocal {
- private Hashtable table = new Hashtable();
-
- /**
- * Creates a ThreadLocal variable.
- */
- public ThreadLocal() {
- }
-
- /**
- * Returns the calling thread's initial value for this ThreadLocal
- * variable. This method will be called once per accessing thread for
- * each ThreadLocal, the first time each thread accesses the variable
- * with get or set. If the programmer desires ThreadLocal variables
- * to be initialized to some value other than null, ThreadLocal must
- * be subclassed, and this method overridden. Typically, an anonymous
- * inner class will be used. Typical implementations of initialize
- * will call an appropriate constructor and return the newly constructed
- * object.
- */
- protected Object initialize() {
- return null;
- }
-
- /**
- * Returns the calling thread's instance of this ThreadLocal variable.
- * Initializes the instance if this is the first time the thread
- * has called this method.
- */
- public Object get() {
- Key key = new Key(Thread.currentThread(), this);
- Object ourVal = table.get(key);
- if (ourVal == null) {
- ourVal = initialize();
- table.put(key, ourVal);
- }
- return ourVal;
- }
-
- /**
- * Sets the calling thread's instance of this ThreadLocal variable
- * to the given value. This is only used to change the value from
- * the one assigned by the initialize method, and most applications
- * will have no need for this functionality.
- */
- public void set(Object value) {
- Key key = new Key(Thread.currentThread(), this);
- table.put(key, value);
- }
-
- /**
- * This "struct" class serves as a Hash Key for a Thread's copy
- * of a ThreadLocal variable.
- */
- private static class Key {
- private Thread thread;
- private ThreadLocal variable;
-
- Key(Thread thread, ThreadLocal variable) {
- this.thread = thread;
- this.variable = variable;
- }
-
- public int hashCode() {
- return thread.hashCode() ^ variable.hashCode();
- }
-
- public boolean equals(Object o) {
- return o instanceof Key && ((Key)o).thread.equals(thread)
- && ((Key)o).variable.equals(variable);
- }
- }
- }
-