Java线程变量ThreadLocal源码分析

Jennifer ·
更新时间:2024-09-20
· 26 次阅读

1.ThreadLocal 线程变量,和当前线程绑定的,只保存当前线程的变量,对于其他线程是隔离的,是访问不到里面的数据的。

2.在Looper中使用到了ThreadLocal,创建了一个Looper是保存到了ThreadLocal中。

//这里用到了泛型,ThreadLocal中只保存Looper对象。 static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); private static void prepare(boolean quitAllowed) { if (sThreadLocal.get() != null) { //保证Looper只被创建一次。 throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed)); }

看下sThreadLocal.set()方法是如何保存数据的。

先拿到当前线程,然后在拿到该线程的ThreadLocalMap成员变量,然后保存到这个map中,

key就是创建的ThreadLocal对象,value就是传进来的value。

public void set(T value) { //拿到当前线程 Thread t = Thread.currentThread(); //得到一个map ThreadLocalMap map = getMap(t); if (map != null){ // 这个map是以当前对象为key的,这个this就是 ThreadLocal的实例 sThreadLocal map.set(this, value); }else{ createMap(t, value); } } //getMap 是从Thread中拿到了一个threadLocals变量,是ThreadLocal.ThreadLocalMap 的实例。 //保存的数据也是存在了这个map中,这也就是为什么ThreadLocal是和线程绑定的,对其他线程来说是隔离的原因所在。 ThreadLocalMap getMap(Thread t) { return t.threadLocals; }

1)保存数据,如果map不为空的情况。走上面if判断的第一个分支。这个存储方式和HashMap类似

private void set(ThreadLocal<?> key, Object value) { Entry[] tab = table; int len = tab.length; // 计算出key在集合中的索引,index int i = key.threadLocalHashCode & (len-1); //开始遍历整个数组, //取出索引为i的Entry,如果不为空,取出下一个,进行遍历 for (Entry e = tab[i]; e != null; e = tab[i = nextIndex(i, len)]) { ThreadLocal<?> k = e.get(); //如果取出的k和传进来的key一致,则把新的值存起来。 if (k == key) { e.value = value; return; } //直到取出最有一个,k==null则进行存储。 if (k == null) { replaceStaleEntry(key, value, i); return; } } //如果索引i的位置,没有Entry,则把传进来的key和value保存在这个位置。 tab[i] = new Entry(key, value); int sz = ++size; //如果大于阈值了,则进行扩容 if (!cleanSomeSlots(i, sz) && sz >= threshold) rehash(); }

2)保存数据,如果map为空的情况。则创建ThreadLocalMap,并赋值给当前线程t。

void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue); } ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) { //创建一个大小为16的数组 table = new Entry[INITIAL_CAPACITY]; //计算得到的i是数组的角标。可以参考hashMap源码分析 int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1); //赋值,保存数据 table[i] = new Entry(firstKey, firstValue); size = 1; //扩容的阈值 setThreshold(INITIAL_CAPACITY); }

3.再看看ThreadLocal是如何取值的。

也是先拿到当前线程t,然后通过t拿到他的成员变量ThreadLocalMap。

public T get() { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); //如果map不为空,则从map中取值 if (map != null) { ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; } } //如果map为空 return setInitialValue(); }

1)如果map不为空则从map中取值。

//如果map不为空 private Entry getEntry(ThreadLocal<?> key) { //拿到key对应的索引 int i = key.threadLocalHashCode & (table.length - 1); //从数组中拿到Entry Entry e = table[i]; if (e != null && e.get() == key){如果key一样直接返回 return e; }else{//如果不一致则开始遍历 return getEntryAfterMiss(key, i, e); } } 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; }

2)如果在get时,得到的map是空的,则这个时候需要初始化

//如果map为空,则调用这个方法,initialValue由用户去实现。 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; } //下面是Choreographer中的例子: private static final ThreadLocal<Choreographer> sThreadInstance = new ThreadLocal<Choreographer>() { @Override protected Choreographer initialValue() { Looper looper = Looper.myLooper(); if (looper == null) { throw new IllegalStateException("The current thread must have a looper!"); } Choreographer choreographer = new Choreographer(looper, VSYNC_SOURCE_APP); if (looper == Looper.getMainLooper()) { mMainInstance = choreographer; } return choreographer; } };

总结ThreadLocal是通过 ThreadLocalMap 进行数据的存储的。而这个ThreadLocalMap对象是通过

获取到当前线程,并从当前线程中拿到的。所以ThreadLocalMap只保存本线程的数据,做到了线程隔离。

ThreadLocalMap存数据的key是ThreadLocal对象本身。 map.set(this, value);

如果想要给ThreadLocalMap中存更多的数据,则需要创建多个对象。

到此这篇关于Java线程变量ThreadLocal源码分析的文章就介绍到这了,更多相关Java ThreadLocal内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



threadlocal JAVA

需要 登录 后方可回复, 如果你还没有账号请 注册新账号