关于jdk1.7中的ThreadLocal

本文详细介绍了JDK1.7中ThreadLocal的get()方法的工作流程,包括获取当前线程、访问ThreadLocalMap、弱引用的使用以及Entry对象的查找与初始化。通过分析源码,揭示了ThreadLocal如何在线程局部存储和检索变量,并探讨了其内存管理机制。

1.get()方法

 /**
     * 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();
    }

第一步先获取到当前的线程

第二步通过getmap获取到当前线程的threadLocals变量。

  /**
     * 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;
    }

在Thread中是这么定义的   

 /* ThreadLocal values pertaining to this thread. This map is maintained
     * by the ThreadLocal class. */
    ThreadLocal.ThreadLocalMap threadLocals = null;

返回的是一个 ThreadLocal中的ThreadLocalMap对象,并且没有任何修饰符,由于ThreadLocal和Thread都属于   package java.lang;  所以这两个可以互相访问



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;
            }
        }

继续观察ThreadLocal的get()方法。

 if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null)
                return (T)e.value;
      }

Entry 这里集成的是  WeakReference<ThreadLocal>,所以根据传入的ThreadLocal对象可以获取到Entry.当不为空的时候就可以返回了。否则就初始化一个参数。这里的ThreadLocal为多线程共享的,所以这里的引用会一直存在,当ThreadLocal置为空时候,对应的Entry对象在下一次的垃圾回收时候就被释放掉了。

通过getmap可以获取到当前线程的ThreadLocalMap对象,但是ThreadLocalMap对象中可能会有其他的ThreadLocal对象的副本数据。 ThreadLocalMap.Entry e = map.getEntry(this); 这句的意思应该是获取到了属于当前ThreadLocal在当前线程中的副本对象。否则就通过 setInitialValue  方法来初始化ThreadLocal的值。

  首先我们来看getEntry这个方法,

   private Entry getEntry(ThreadLocal key) {
            int i = key.threadLocalHashCode & (table.length - 1);
            Entry e = table[i];
            if (e != null && e.get() == key)
                return e;
            else
                return getEntryAfterMiss(key, i, e);

        }

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

以上的tabLe位于ThreadLocalMap类中,由定义可以知道table必须为2的倍数,这样做的话可以使int i = key.threadLocalHashCode & (table.length - 1);  这里的i的值为hashcode的尾数(后table中定义的倍数位 )由这里看来i应该是当前的ThreadLocal位于thread中的ThreadLocalMap的table中的的索引位置(这里接下来需要验证)。这里就可以返回值了,不为空就返回值,否则执行getEntryAfterMiss方法,接下来查看这里的代码。

 /**
         * Version of getEntry method for use when key is not found in
         * its direct hash slot.
         *
         * @param  key the thread local object
         * @param  i the table index for key's hash code
         * @param  e the entry at table[i]
         * @return the entry associated with key, or null if no such
         */
        private Entry getEntryAfterMiss(ThreadLocal key, int i, Entry e) {
            Entry[] tab = table;
            int len = tab.length;


            while (e != null) {
                ThreadLocal k = e.get();
                if (k == key)
                    return e;
                if (k == null)
                    expungeStaleEntry(i);
                else
                    i = nextIndex(i, len);
                e = tab[i];
            }
            return null;
        }







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值