您当前的位置: 首页 > 
  • 0浏览

    0关注

    674博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ThreadLocal学习--存储结构(二)

沙漠一只雕得儿得儿 发布时间:2020-02-01 17:33:52 ,浏览量:0

为了更深刻理解ThreadLocal对值的存储,我们先来看下面的例子,这个是在Test下建立的测试用例:

import org.junit.Test;

public class DNThreadLocalTest {

    @Test
    public void test(){
        //---------------------------------------------------创建第一个threadLocal
        //创建一个本地线程(主线程)
        final ThreadLocal threadLocal = new ThreadLocal(){
            @Override
            protected String initialValue() {
                //重写初始化方法,默认返回null
                return "jett老师";
            }
        };

        System.out.println("主线程threadLocal:" + threadLocal.get());  //jett老师

        //---------------------------------------------------创建第二个threadLocal2
        final ThreadLocal threadLocal2 = new ThreadLocal();

        //---------------------------------------------------thread-1
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                //从ThreadLocalMap key=thread-1的值为空 ? 没有值  就走 initialValue() 初始化方法
                String local1_value1 = threadLocal.get();
                Integer local2_value1 = threadLocal2.get();
                System.out.println("thread-1:" + "ThreadLocal1: " + local1_value1 + " ThreadLocal2:" + local2_value1);  //jett老师

                threadLocal.set("大卫老师");
                threadLocal2.set(10010);
                String local1_value2 = threadLocal.get();
                Integer local2_value2 = threadLocal2.get();
                System.out.println("thread-1:" + "ThreadLocal1: " + local1_value2 + " ThreadLocal2:" + local2_value2);  //大卫老师

                //避免大量无意义得内存占用
                threadLocal.remove();
            }
        });
        thread.start();

        //---------------------------------------------------thread-2
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                //从ThreadLocalMap key=thread-2得值 ? 没有  就走 initialValue() 初始化方法
                String value1 = threadLocal.get();
                System.out.println("thread-2:" + value1);  //jett老师

                threadLocal.set("Alan老师");
                String value2 = threadLocal.get();
                System.out.println("thread-2:" + value2);  //Alan老师

                //避免大量无意义得内存占用
                threadLocal.remove();
            }
        });
        thread2.start();
    }
}

执行的结果如下:

主线程threadLocal:jett老师
thread-1:ThreadLocal1: jett老师 ThreadLocal2:null
thread-1:ThreadLocal1: 大卫老师 ThreadLocal2:10010
thread-2:jett老师
thread-2:Alan老师

ThreadLocal

threadlocal使用方法很简单

static final ThreadLocal sThreadLocal = new ThreadLocal();
sThreadLocal.set()
sThreadLocal.get()

threadlocal而是一个线程内部的存储类,可以在指定线程内存储数据,数据存储以后,只有指定线程可以得到存储数据,官方解释如下。

/**
 * This class provides thread-local variables.  These variables differ from
 * their normal counterparts in that each thread that accesses one (via its
 * {@code get} or {@code set} method) has its own, independently initialized
 * copy of the variable.  {@code ThreadLocal} instances are typically private
 * static fields in classes that wish to associate state with a thread (e.g.,
 * a user ID or Transaction ID).
 */

大致意思就是ThreadLocal提供了线程内存储变量的能力,这些变量不同之处在于每一个线程读取的变量是对应的互相独立的。通过get和set方法就可以得到当前线程对应的值。

做个不恰当的比喻,从表面上看ThreadLocal相当于维护了一个map,key就是当前的线程,value就是需要存储的对象。

这里的这个比喻是不恰当的,实际上是ThreadLocal的静态内部类ThreadLocalMap为每个Thread都维护了一个数组table,ThreadLocal确定了一个数组下标,而这个下标就是value存储的对应位置。。

作为一个存储数据的类,关键点就在get和set方法。

//set 方法
public void set(T value) {
      //获取当前线程
      Thread t = Thread.currentThread();
      //实际存储的数据结构类型
      ThreadLocalMap map = getMap(t);
      //如果存在map就直接set,没有则创建map并set
      if (map != null)
          map.set(this, value);
      else
          createMap(t, value);
  }
  
//getMap方法
ThreadLocalMap getMap(Thread t) {
      //thred中维护了一个ThreadLocalMap
      return t.threadLocals;
 }
 
//createMap
void createMap(Thread t, T firstValue) {
      //实例化一个新的ThreadLocalMap,并赋值给线程的成员变量threadLocals
      t.threadLocals = new ThreadLocalMap(this, firstValue);
}

从上面代码可以看出每个线程持有一个ThreadLocalMap对象。每一个新的线程Thread都会实例化一个ThreadLocalMap并赋值给成员变量threadLocals,使用时若已经存在threadLocals则直接使用已经存在的对象。

Thread

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

Thread中关于ThreadLocalMap部分的相关声明,接下来看一下createMap方法中的实例化过程。

ThreadLocalMap

set方法

//Entry为ThreadLocalMap静态内部类,对ThreadLocal的若引用
//同时让ThreadLocal和储值形成key-value的关系
static class Entry extends WeakReference k = e.get();

                if (k == key) {
                    e.value = value;
                    return;
                }

                if (k == null) {
                    replaceStaleEntry(key, value, i);
                    return;
                }
            }
            
            //如果上面没有遍历成功则创建新值
            tab[i] = new Entry(key, value);
            int sz = ++size;
            //满足条件数组扩容x2
            if (!cleanSomeSlots(i, sz) && sz >= threshold)
                rehash();
        }

在ThreadLocalMap中的set方法与构造方法能看到以下代码片段。

  • int i = key.threadLocalHashCode & (len-1)
  • int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1) 简而言之就是将threadLocalHashCode进行一个位运算(取模)得到索引i,threadLocalHashCode代码如下。
    //ThreadLocal中threadLocalHashCode相关代码.
    
    private final int threadLocalHashCode = nextHashCode();

    /**
     * The next hash code to be given out. Updated atomically. Starts at
     * zero.
     */
    private static AtomicInteger nextHashCode =
        new AtomicInteger();

    /**
     * The difference between successively generated hash codes - turns
     * implicit sequential thread-local IDs into near-optimally spread
     * multiplicative hash values for power-of-two-sized tables.
     */
    private static final int HASH_INCREMENT = 0x61c88647;

    /**
     * Returns the next hash code.
     */
    private static int nextHashCode() {
        //自增
        return nextHashCode.getAndAdd(HASH_INCREMENT);
    }

因为static的原因,在每次new ThreadLocal时因为threadLocalHashCode的初始化,会使threadLocalHashCode值自增一次,增量为0x61c88647。

0x61c88647是斐波那契散列乘数,它的优点是通过它散列(hash)出来的结果分布会比较均匀,可以很大程度上避免hash冲突,已初始容量16为例,hash并与15位运算计算数组下标结果如下:

hashCode数组下标0x61c8864770xc3910c8e140x255992d550x8722191c120xe8ea9f6330x4ab325aa100xac7babf110xe44323880x700cb87f15

总结如下:

  1. 对于某一ThreadLocal来讲,他的索引值i是确定的,在不同线程之间访问时访问的是不同的table数组的同一位置即都为table[i],只不过这个不同线程之间的table是独立的。
  2. 对于同一线程的不同ThreadLocal来讲,这些ThreadLocal实例共享一个table数组,然后每个ThreadLocal实例在table中的索引i是不同的。

get()方法

//ThreadLocal中get方法
public T get() {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null) {
        ThreadLocalMap.Entry e = map.getEntry(this);
        if (e != null) {
            @SuppressWarnings("unchecked")
            T result = (T)e.value;
            return result;
        }
    }
    return setInitialValue();
}
    
//ThreadLocalMap中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);
   }

理解了set方法,get方法也就清楚明了,无非是通过计算出索引直接从数组对应位置读取即可。

ThreadLocal实现主要涉及Thread,ThreadLocal,ThreadLocalMap这三个类。关于ThreadLocal的实现流程正如上面写的那样,实际代码还有许多细节处理的部分并没有在这里写出来。

ThreadLocal特性

ThreadLocal和Synchronized都是为了解决多线程中相同变量的访问冲突问题,不同的点是

  • Synchronized是通过线程等待,牺牲时间来解决访问冲突
  • ThreadLocal是通过每个线程单独一份存储空间,牺牲空间来解决冲突,并且相比于Synchronized,ThreadLocal具有线程隔离的效果,只有在线程内才能获取到对应的值,线程外则不能访问到想要的值。

正因为ThreadLocal的线程隔离特性,使他的应用场景相对来说更为特殊一些。在android中Looper、ActivityThread以及AMS中都用到了ThreadLocal。当某些数据是以线程为作用域并且不同线程具有不同的数据副本的时候,就可以考虑采用ThreadLocal。

链接:https://www.jianshu.com/p/3c5d7f09dfbd

关注
打赏
1657159701
查看更多评论
立即登录/注册

微信扫码登录

0.0400s