ThreadLocal Java多线程下的影分身之术
  TEZNKK3IfmPf 2023年11月12日 24 0

如果写过多线程的代码,你肯定考虑过线程安全问题,更进一步你可能还考虑在在线程安全的前提下性能的问题。大多数情况下大家用来解决线程安全问题都会使用同步,比如用synchron或者concurrent包提供的各种锁,当然这些都能解决问题。但有多线程做同步一定会涉及到资源争抢和等待的问题。java中各种同步方法都是提供一种准入机制,JVM会调用系统同步原语来保证临界区任意时刻只能有一个线程进入,那必然其他线程都得等待了,性能的瓶颈就在这同步上了。
  解决问题最好的方式是啥?当然是避免问题的发生了。ThreadLocal就是用这样一种方式提升性能的。ThreadLocal遍历会为每个线程单独维护一份值,某个线程对其做任何操作都不会影响其他的线程,这相当于这个对象在每个线程下面都有了一个分身。ThreadLocal是以Thread为维度实现的,所以多线程之间也不会有争抢和等待,从而避免同步变成瓶颈,下文我们会从源码的维度去看这些都是如何实现的。
  ThreadLocal也不是万金油,它也只能在多线程之间数据相互独立的情况下使用,如果是多线程间的数据同步,还得使用某个同步的方式。 我的理解,ThreadLocal是在临时变量完全不共享和全部变量完全共享之间取了个折中,在多线程数据一致的情况下完美的避免了资源争抢和等待,提高了性能。

ThreadLocal的使用也很简单,直接​​new ThreadLocal<T>();​​​ 就可以了,然后可以通过set()和get()分别设值和获取值。以下代码展示我如果使用​​ThreadLocal<Integer>​​如何为每个线程单独维护一个值的,而且线程之间也不会相互干扰。

public class Demo extends Thread {
private static ThreadLocal<Integer> counter = new ThreadLocal<>();

@Override
public void run() {
counter.set(ThreadLocalRandom.current().nextInt(100));
System.out.println(Thread.currentThread().getName() + ":" + counter.get());
try {
Thread.sleep(2000 + ThreadLocalRandom.current().nextInt(3000));
} catch (Exception e) {
e.printStackTrace();
}
counter.set(counter.get()+100);
System.out.println(Thread.currentThread().getName() + ":" + counter.get());
}

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new Demo();
thread.setName("Thread" + i);
thread.start();
}
}
}

上面我用到了ThreadLocal的set和get方法,其运行结果如下,因为使用了随机数,可能每次运行解决会不一致。可以很明显看得出,虽然多线对统一个Object操作,但却没有影响到各自的值。

Thread1:42
Thread0:20
Thread2:18
Thread3:6
Thread4:76
Thread5:50
Thread6:81
Thread7:75
Thread8:48
Thread9:56
Thread4:176
Thread7:175
Thread1:142
Thread6:181
Thread2:118
Thread5:150
Thread0:120
Thread3:106
Thread9:156

除了set和get接口外,ThreadLocal还提供了remove(),该方法可以将当前线程的所有内容清除掉。另外还有一个​​ThreadLocal<S> withInitial()​​。

ThreadLocal Java多线程下的影分身之术

源码分析

接下来我们就从源码来剖析下ThreadLocal是如何实现不同线程下不同值的,首先我们来看下set()方法,这是我们在除了构造函数外第一个用的方法,它也承担着ThreadLocal初始化的任务。

public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
map.set(this, value);
} else {
createMap(t, value);
}
}
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}

set()也非常简单,我顺便也把set()涉及到的两个方法贴上。set()首先获取当前线程t,然后从t中获取ThreadLocalMap,如果ThreadLocalMap为空就创建一个。ThreadLocalMap是ThreadLocal中比较核心的东西,稍后会详细介绍。上面代码很显然,ThreadLocalMap是将ThreadLocal作为map的key。虽然多线程下都是用同一个ThreadLocal对象作为Key的,但每次获取key对应的Value是从不同的Map中获取,

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

虽然多线程下都是用同一个ThreadLocal对象作为Key的,但每次获取key对应的Value是从不同的Map中获取,这就保证了多下次下value不会冲突。get方法在ThreadLocalMap未创建的情况下,还会调用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);
}
if (this instanceof TerminatingThreadLocal) {
TerminatingThreadLocal.register((TerminatingThreadLocal<?>) this);
}
return value;
}

从代码上来看,其实就是初始化一下ThreadLocalMap然后返回一个默认的初始值null,这个和set的初始化很像,为什么不用set呢?注释里也解释了,是为了防止set()被重载,导致get方法的初始化失效。我感觉单纯从实现ThreadLocal上来说,get中最后不return setInitialValue();而是return null也是可以的。不是很理解代码作者为什么要这么写。
  ThreadLocal主要功能就是这么实现的,说白了就是对ThreadLocalMap的操作。ThreadLocalMap才是核心。

ThreadLocalMap

我总结看Java代码的方法,就是先看类的声明,然后按实际用途从每个方法入手看是怎么执行的。

static class ThreadLocalMap { }
```  
  ThreadLocalMap是直接声明在ThreadLocal内部的,其他地方就没法用了(其实外部也没必要用,轮map的功能,它实现也没有HashMap和Tree好)。另外,它没有实现Map接口,emmm 这就意味它不是一个标准的map了。
```java
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;

Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}

ThreadLocalMap的Entry继承了WeakReference,这让我想到了​​WeakHashMap​​,这里用WeakReference的原因也很明确,就是想让Key在失效后,Map能主动清理相关的Entry。

ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
table = new Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY);
}
private void setThreshold(int len) {
threshold = len * 2 / 3;
}

ThreadLocalMap也有几个默认参数,初始容量INITIAL_CAPACITY,threshold是容量的2/3,就是如果Map中的Entry数量超过总容量的2/3,ThreadLocalMap对进行扩容。

private void set(ThreadLocal<?> key, Object value) {

// We don't use a fast path as with get() because it is at
// least as common to use set() to create new entries as
// it is to replace existing ones, in which case, a fast
// path would fail more often than not.

Entry[] tab = table;
int len = tab.length;
int i = key.threadLocalHashCode & (len-1);

for (Entry e = tab[i];
e != null;
e = tab[i = nextIndex(i, len)]) {
ThreadLocal<?> 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;
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}

从set方法中我们就可以看出ThreadLocalMap和HashMap,TreeMap的设计不同之处。首先也是对Key求hash值做定位,但当遇到hash冲突的时候,它的选择不是开链,而是调用nextIndex往后移动,直到遇见某个entry为null或者其key和要插入的key一样。同时,插入的过程也会调用replaceStaleEntry对Map做清理,清理过程比较复杂,我们稍后说。插入后,如果size大于阀值,也会对整个map做扩容操作。

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

因为刚刚说到ThreadLocalMap处理key冲突的方式是往后移,直到有空闲的位置。这样虽然实现简单,但查的时候问题就来了,根据hash值算出来的位置没有,并不意味着整个map里没有,所以得往后遍历,直到找到或者遍历到某个空Entry。如果你仔细想想可能就会发现问题,如果只是遍历到遇到null,而不是遍历整个tab,可能会漏掉。比如下面这个例子。

  | 0 | 1 | 2 | 3 | 5 | 6 | 7 |  
| | a | b | c | d | e | |   

开始的时候,tab状态是这样的,现在我要插入一个h,其hashcode恰好是1,然而a已经在那了,按插入逻辑,h只能插到7的位置了,插入后如下。

  | 0 | 1 | 2 | 3 | 5 | 6 | 7 |  
| | a | b | c | d | e | h |   

后来,我把c删掉,变成了下面这样。如果我现在想查h,按照上面getEntry的逻辑,是不是遍历到3就停了,所以找不到h了? getEntry的逻辑表面确实是这样,但实际上getEntryAfterMiss、remove、gets时都会直接或者间接调用expungeStaleEntry会对表里的数据做整理。expungeStaleEntry()除了利用弱引用的特性对tab中Entry做清理外,还会对之前Hash冲突导致后移的Entry重新安放位置。所以不可能出现下面这种tab排放的。

  | 0 | 1 | 2 | 3 | 5 | 6 | 7 |  
| | a | b | | d | e | h |   
private int expungeStaleEntry(int staleSlot) {
Entry[] tab = table;
int len = tab.length;

// expunge entry at staleSlot
tab[staleSlot].value = null;
tab[staleSlot] = null;
size--;

// Rehash until we encounter null
Entry e;
int i;
for (i = nextIndex(staleSlot, len);
(e = tab[i]) != null;
i = nextIndex(i, len)) {
ThreadLocal<?> k = e.get();
if (k == null) {
e.value = null;
tab[i] = null;
size--;
} else {
int h = k.threadLocalHashCode & (len - 1);
if (h != i) {
tab[i] = null;

// Unlike Knuth 6.4 Algorithm R, we must scan until
// null because multiple entries could have been stale.
while (tab[h] != null)
h = nextIndex(h, len);
tab[h] = e;
}
}
}
return i;
}

还有set中调用的replaceStaleEntry(),代码很长,其实也是保证key失效的Entry被清理,Hash冲突的key能放回正确的位置。

private void replaceStaleEntry(ThreadLocal<?> key, Object value,
int staleSlot) {
Entry[] tab = table;
int len = tab.length;
Entry e;

// Back up to check for prior stale entry in current run.
// We clean out whole runs at a time to avoid continual
// incremental rehashing due to garbage collector freeing
// up refs in bunches (i.e., whenever the collector runs).
int slotToExpunge = staleSlot;
for (int i = prevIndex(staleSlot, len);
(e = tab[i]) != null;
i = prevIndex(i, len))
if (e.get() == null)
slotToExpunge = i;

// Find either the key or trailing null slot of run, whichever
// occurs first
for (int i = nextIndex(staleSlot, len);
(e = tab[i]) != null;
i = nextIndex(i, len)) {
ThreadLocal<?> k = e.get();

// If we find key, then we need to swap it
// with the stale entry to maintain hash table order.
// The newly stale slot, or any other stale slot
// encountered above it, can then be sent to expungeStaleEntry
// to remove or rehash all of the other entries in run.
if (k == key) {
e.value = value;

tab[i] = tab[staleSlot];
tab[staleSlot] = e;

// Start expunge at preceding stale entry if it exists
if (slotToExpunge == staleSlot)
slotToExpunge = i;
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
return;
}

// If we didn't find stale entry on backward scan, the
// first stale entry seen while scanning for key is the
// first still present in the run.
if (k == null && slotToExpunge == staleSlot)
slotToExpunge = i;
}

// If key not found, put new entry in stale slot
tab[staleSlot].value = null;
tab[staleSlot] = new Entry(key, value);

// If there are any other stale entries in run, expunge them
if (slotToExpunge != staleSlot)
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
}

看这么多复杂的代码,最后看个简单的resize(),ThreadLocalMap的resize相较于HashMap的简单多了,就是新建一个长度为当前2倍的tab,然后把当前tab中的每个entry重新计算index再插入新tab。

private void resize() {
Entry[] oldTab = table;
int oldLen = oldTab.length;
int newLen = oldLen * 2;
Entry[] newTab = new Entry[newLen];
int count = 0;

for (Entry e : oldTab) {
if (e != null) {
ThreadLocal<?> k = e.get();
if (k == null) {
e.value = null; // Help the GC
} else {
int h = k.threadLocalHashCode & (newLen - 1);
while (newTab[h] != null)
h = nextIndex(h, newLen);
newTab[h] = e;
count++;
}
}
}

setThreshold(newLen);
size = count;
table = newTab;
}

看来看去,ThreadLocalMap想要实现的功能和​​WeakHashMap​​​类似,为什么不直接使用​​WeakHashMap​​呢!!

使用场景

  1. 数据库连接
  2. Cache
  3. 线程池

参考资料

  1. ThreadLocal源码
  2. ​简单理解ThreadLocal原理和适用场景,多数据源下ThreadLocal的应用​
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   20天前   44   0   0 java
  TEZNKK3IfmPf   2024年05月31日   54   0   0 java
TEZNKK3IfmPf