`

ThreadLocal原理及使用

    博客分类:
  • java
阅读更多

一,实现原理

        ThreadLocal,ThreadLocalMap(ThreadLocal的静态内部类),Thread三个类。

       

 

       1,在每个Thread实例中,都有一个threadlocals成员属性,用于存储该线程内的数据。

         

public class Thread implements Runnable {
   /* ThreadLocal values pertaining to this thread. This map is maintained
     * by the ThreadLocal class. */
    ThreadLocal.ThreadLocalMap threadLocals = null;
}

       2,ThreadLocalMap是一个Map结构,key为ThreadLocal类型,value为Object类型。

      

public class ThreadLocal<T> {

    /**
     * Returns the value in the current thread's copy of this
     * thread-local variable.  If the variable has no value for the
     * current thread, it is first initialized to the value returned
     * by an invocation of the {@link #initialValue} method.
     * @return the current thread's value of this thread-local
     */
    public T get() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null)
                return (T)e.value;
        }
        return setInitialValue();
    }

    /**
     * Variant of set() to establish initialValue. Used instead
     * of set() in case user has overridden the set() method.
     * @return the initial value
     */
    private T setInitialValue() {
        T value = initialValue();
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
        return value;
    }

    /**
     * Sets the current thread's copy of this thread-local variable
     * to the specified value.  Most subclasses will have no need to 
     * override this method, relying solely on the {@link #initialValue}
     * method to set the values of thread-locals.
     * @param value the value to be stored in the current thread's copy of
     * this thread-local.
     */
    public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }

    /**
     * Removes the current thread's value for this thread-local
     * variable.  If this thread-local variable is subsequently
     * {@linkplain #get read} by the current thread, its value will be
     * reinitialized by invoking its {@link #initialValue} method,
     * unless its value is {@linkplain #set set} by the current thread
     * in the interim.  This may result in multiple invocations of the
     * <tt>initialValue</tt> method in the current thread.
     * @since 1.5
     */
     public void remove() {
         ThreadLocalMap m = getMap(Thread.currentThread());
         if (m != null)
             m.remove(this);
     }

    /**
     * Get the map associated with a ThreadLocal. Overridden in
     * InheritableThreadLocal.
     * @param  t the current thread
     * @return the map
     */
    ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }
 
     /**
     * Create the map associated with a ThreadLocal. Overridden in
     * InheritableThreadLocal.
     * @param t the current thread
     * @param firstValue value for the initial entry of the map
     * @param map the map to store.
     */
    void createMap(Thread t, T firstValue) {
        t.threadLocals = new ThreadLocalMap(this, firstValue);
    }
 
   /**
     * ThreadLocalMap is a customized hash map suitable only for
     * maintaining thread local values. No operations are exported
     * outside of the ThreadLocal class. The class is package private to
     * allow declaration of fields in class Thread.  To help deal with
     * very large and long-lived usages, the hash table entries use
     * WeakReferences for keys. However, since reference queues are not
     * used, stale entries are guaranteed to be removed only when
     * the table starts running out of space.
     */
    static class ThreadLocalMap {

        /**
         * The entries in this hash map extend WeakReference, using
         * its main ref field as the key (which is always a
         * ThreadLocal object).  Note that null keys (i.e. entry.get()
         * == null) mean that the key is no longer referenced, so the
         * entry can be expunged from table.  Such entries are referred to
         * as "stale entries" in the code that follows.
         */
        static class Entry extends WeakReference<ThreadLocal> {
            /** The value associated with this ThreadLocal. */
            Object value;

            Entry(ThreadLocal k, Object v) {
                super(k);
                value = v;
            }
        }
        
        private static final int INITIAL_CAPACITY = 16;

        /**
         * The table, resized as necessary.
         * table.length MUST always be a power of two.
         */
        private Entry[] table;

    }
  }
}

     在每个线程Thread实例中都有一个存放本线程内数据的ThreadLocalMap【Map类型】成员变量,其中key为ThreadLocal实例,value为Object类型,所以一个线程下可以保存多个"线程局部变量"。当调用ThreadLocal中的set(T value)方法时,实际上是将该ThreadLocal实例作为key,存入当前运行线程中的数据区ThreadLocalMap【Map类型】中了,这样就保证了每个线程都有自己的一个副本,不会相互影响。

  
 二,应用场景

     Struts2相对于Struts1的一个重要升级是对request,response两个对象的解耦,Struts2的Action方法中不再需要传递 request,response参数。Struts2不通过方法直接传入request,response对象,那么这两个值是如何传递的呢?Struts2采用的正是ThreadLocal变量。在每次接收到请求时,Struts2在调用拦截器和action前,通过将 request,response对象放入ActionContext实例中,而ActionContext实例则作为”线程局部变量”存入当前线程的ThreadLocalMap成员变量中key=ThreadLocal实例 ,value=actionContext。【作用:参数隐传,达到解耦的目的将一个共用的ThreadLocal静态实例作为key,将不同对象的引用保存到不同线程的ThreadLocalMap中,然后在线程执行的各处通过这个静态ThreadLocal实例的get()方法取得自己线程保存的那个对象,避免了将这 个对象作为参数传递的麻烦。

  • 大小: 79.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics