home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / lang / ThreadLocal.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  3.4 KB  |  109 lines

  1. /*
  2.  * @(#)ThreadLocal.java    1.3 98/03/18
  3.  *
  4.  * Copyright 1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16. import java.util.Hashtable;
  17.  
  18. /**
  19.  * This class provides ThreadLocal variables.  These variables differ from
  20.  * their normal counterparts in that each thread that accesses one (via its
  21.  * get or set method) has its own, independently initialized copy of the
  22.  * variable.  ThreadLocal objects are typically private static variables in
  23.  * classes that wish to associate state with a thread (e.g., a user ID or
  24.  * Transaction ID).
  25.  *
  26.  * Each thread holds an implicit.reference to its copy of a ThreadLocal
  27.  * as long as the thread is alive and the ThreadLocal object is accessible;
  28.  * after a thread goes away, all of its copies of ThreadLocal variables are
  29.  * subject to garbage collection (unless other references to these copies
  30.  * exist).
  31.  *
  32.  * @author  Josh Bloch
  33.  * @version 1.3 03/18/98
  34.  */
  35.  
  36. public class ThreadLocal {
  37.     private Hashtable table = new Hashtable();
  38.  
  39.     /**
  40.      * Creates a ThreadLocal variable.
  41.      */
  42.     public ThreadLocal() {
  43.     }
  44.  
  45.     /**
  46.      * Returns the calling thread's initial value for this ThreadLocal
  47.      * variable. This method will be called once per accessing thread for
  48.      * each ThreadLocal, the first time each thread accesses the variable
  49.      * with get or set.  If the programmer desires ThreadLocal variables
  50.      * to be initialized to some value other than null, ThreadLocal must
  51.      * be subclassed, and this method overridden.  Typically, an anonymous
  52.      * inner class will be used.  Typical implementations of initialize
  53.      * will call an appropriate constructor and return the newly constructed
  54.      * object.
  55.      */
  56.     protected Object initialize() {
  57.     return null;
  58.     }
  59.  
  60.     /**
  61.      * Returns the calling thread's instance of this ThreadLocal variable.
  62.      * Initializes the instance if this is the first time the thread
  63.      * has called this method.
  64.      */
  65.     public Object get() {
  66.     Key key = new Key(Thread.currentThread(), this);
  67.     Object ourVal = table.get(key);
  68.     if (ourVal == null) {
  69.         ourVal = initialize();
  70.         table.put(key, ourVal);
  71.         }
  72.     return ourVal;
  73.     }
  74.  
  75.     /**
  76.      * Sets the calling thread's instance of this ThreadLocal variable
  77.      * to the given value.  This is only used to change the value from
  78.      * the one assigned by the initialize method, and most applications
  79.      * will have no need for this functionality.
  80.      */
  81.     public void set(Object value) {
  82.     Key key = new Key(Thread.currentThread(), this);
  83.     table.put(key, value);
  84.     }
  85.  
  86.     /**
  87.      * This "struct" class serves as a Hash Key for a Thread's copy
  88.      * of a ThreadLocal variable.
  89.      */
  90.     private static class Key {
  91.     private Thread        thread;
  92.     private ThreadLocal    variable;
  93.  
  94.     Key(Thread thread, ThreadLocal variable) {
  95.         this.thread = thread;
  96.         this.variable = variable;
  97.     }
  98.  
  99.     public int hashCode() {
  100.         return thread.hashCode() ^ variable.hashCode();
  101.     }
  102.  
  103.     public boolean equals(Object o) {
  104.        return o instanceof Key && ((Key)o).thread.equals(thread)
  105.                           && ((Key)o).variable.equals(variable);
  106.         }
  107.     }
  108. }
  109.