前言:多线程中,如果每个线程都想储存一份变量数据,从而使得改数据在一个线程的生存时间内都可以被访问并修改,当线程结束后释放掉改数据;如场景:当需要获取到当前线程中对应的用户信息,以此方便记录用户的操作日志;那么java 中有什么可以方便我们进行这种业务的实现呢?
1 ThreadLocal 背景:
ThreadLocal,线程局部变量副本,每个线程都拥有自己的变量副本,使得我们可以在高并发的时候做到,线程之间副本数据的隔离性;在高并发场景下,可以实现无状态的调用,特别适用于各个线程依赖不通的变量值完成操作的场景。
2 ThreadLocal 使用:
2.1 声明ThreadLocal :
final static ThreadLocal<String> threadLocal1 = new ThreadLocal<>();
2.2 使用时进行set:
threadLocal1.set("张三");
2.3 使用完成进remove:
threadLocal1.remove();
3 ThreadLocal 原理:
ThreadLocal 为什么可以实现不同线程之间的数据隔离性呢,既然ThreadLocal 常用的只有set(),get(),remove(),那么来看下这3个方法都做了什么事情:
3,1 set():
ThreadLocal
public void set(T value) {
// 获取当前线程
Thread t = Thread.currentThread();
// 获取当前线程的 ThreadLocalMap 属性
ThreadLocalMap map = getMap(t);
if (map != null)
// 如果当前线程的ThreadLocalMap 不为空则直接进行对map 进行key-value 的设置
map.set(this, value);
else
// 如果当前线程的ThreadLocalMap 为空,则进行初始化,并进行进行key-value 的设置
createMap(t, value);
}
第一次先进行初始化并设置entry,createMap(t, value):
void createMap(Thread t, T firstValue) {
// 初始一个ThreadLocalMap 并把他赋值到 Thread 的threadLocals 属性中
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
new ThreadLocalMap(this, firstValue):
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
// 初始化entry 的长度 private static final int INITIAL_CAPACITY = 16;
table = new Entry[INITIAL_CAPACITY];
// 计算当前的ThreadLocal 所在数组下边位置
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
// 当前数组位置赋值
table[i] = new Entry(firstKey, firstValue);
// 初始化 table 的size 为1
size = 1;
// 设置负载因子 threshold = len * 2 / 3;
setThreshold(INITIAL_CAPACITY);
}
new Entry(firstKey, firstValue):
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
super(k):
public class WeakReference<T> extends Reference<T> {
/**
* Creates a new weak reference that refers to the given object. The new
* reference is not registered with any queue.
*
* @param referent object the new weak reference will refer to
*/
public WeakReference(T referent) {
super(referent);
}
/**
* Creates a new weak reference that refers to the given object and is
* registered with the given queue.
*
* @param referent object the new weak reference will refer to
* @param q the queue with which the reference is to be registered,
* or null if registration is not required
*/
public WeakReference(T referent, ReferenceQueue<? super T> q) {
super(referent, q);
}
}
如果已经进行过ThreadLocalMap的初始化,则直接进行设置:
map.set(this, value):
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.
// 获取到当前 ThreadLocalMap的entry 数组
Entry[] tab = table;
// 获取到当前 ThreadLocalMap的entry 数组 长度
int len = tab.length;
// 计算当前ThreadLocal 所在 ThreadLocalMap的entry的数组下标
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) {
// 如果改key 已经过期,则替换和清除过期 的key
replaceStaleEntry(key, value, i);
return;
}
}
// 如果当前数组中没有则新建entry
tab[i] = new Entry(key, value);
int sz = ++size;
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}
3,2 get():
public T get() {
// 获取当前线程
Thread t = Thread.currentThread();
// 获取当前现成的ThreadLocalMap 属性
ThreadLocalMap map = getMap(t);
if (map != null) {
// 获取 ThreadLocalMap的对应数组下的entry
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
// 获取对应的value 并返回
T result = (T)e.value;
return result;
}
}
// 如果ThreadLocalMap 为空则对到当前线程的ThreadLocalMap 初始化化并返回null
return setInitialValue();
}
getMap(t):
ThreadLocalMap getMap(Thread t) {
// 防护当前先到的threadLocals属性
return t.threadLocals;
}
map.getEntry(this):
private Entry getEntry(ThreadLocal<?> key) {
// 更加当前的ThreadLocal 计算所在当前线程ThreadLocalMap entry 数组的位置
int i = key.threadLocalHashCode & (table.length - 1);
// 获取entry
Entry e = table[i];
if (e != null && e.get() == key)
// 再次判断如果key 相同则至直接返回entry
return e;
else
// 如果找不到或者 key 不相同,则遍历向下办理尝试去获取entry
return getEntryAfterMiss(key, i, e);
}
getEntryAfterMiss:
private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) {
Entry[] tab = table;
int len = tab.length;
while (e != null) {
// 获取key
ThreadLocal<?> k = e.get();
if (k == key)
return e;
if (k == null)
// 清除过期key
expungeStaleEntry(i);
else
i = nextIndex(i, len);
e = tab[i];
}
return null;
}
如果ThreadLocalMap 为空则对到当前线程的ThreadLocalMap 初始化化并返回null,setInitialValue:
private T setInitialValue() {
// 初始化value 为null protected T initialValue() {
// return null;
// }
T value = initialValue();
Thread t = Thread.currentThread();
// 获取当前线程的ThreadLocalMap
ThreadLocalMap map = getMap(t);
if (map != null)
// ThreadLocalMap 不为null 则设置对应的key - value
map.set(this, value);
else
// ThreadLocalMap 为null 初始化 ThreadLocalMap
createMap(t, value);
return value;
}
3.3 remove():
public void remove() {
// 获取当前线程的 ThreadLocalMap
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
// 移除当前的线程的ThreadLocal entry
m.remove(this);
}
tip:thread local map底层的Entry数组只会扩容,不会缩容;
4 ThreadLocal 为什么会造成内存泄露:
内存泄漏(Memory Leak)是指程序中已动态分配的堆内存由于某种原因程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果。
ThreadLocalMap 是Thread 中维护的一个属性,其生命周期是跟着Thread 的,如果一个Thread 从创建,使用,到销毁 耗时很短,在Thread 销毁时占用的空间都会被释放,则没有问题;但是Thread 是一种宝贵的资源不可能被频繁的创建和销毁,尤其现在程序中大多数资源都使用了线程池技术,这就导致了一个Thread 的生命周期会变得很长,而Thread 的ThreadLocalMap 又是一个只能扩容不会缩容的容器,那么如果ThreadLocalMap 中的key和value 的引用一直存在,就导致对应的数据堆中无法被jvm 进行回收,从而使的内存被一点点耗尽。
所以基于此就需要对ThreadLocalMap 中无效的key-value 及时的清除,来确保没有强引用一直存在,从而使得jvm及时的进行垃圾回收;
那么基于此我们就要做到:
(1)将ThreadLocal 定义为 final static 避免ThreadLocal 被频繁的创建,创建对象少了jvm 的压力也随之减轻,声明为static,这样就一直存在ThreadLocal的强引用,也就能保证任何时候都能通过ThreadLocal的弱引用访问到Entry的value值,进而清除掉 ;
(2)每次使用完ThreadLocal都调用它的remove()方法清除数据,清除对象的强引用方便jvm 回收;
5 总结:
5.1 ThreadLocal 是通过维护每个Thread 中的ThreadLocalMap来实现不同Thread 可以获取到各自ThreadLocalMap的数据从而实现数据的隔离性;
5.2 :ThreadLocalMap 底层的Entry数组只会扩容,不会缩容;
5.4 :ThreadLocal 在使用完毕后及时调用remove() 方法让其释放掉对应的key和value;
6 扩展:
6.1 既然 ThreadLocal 是通过维护每个Thread 中的ThreadLocalMap来实现数隔离,那么为什么不将ThreadLocalMap 直接放入到Thread 类中而是放到ThreadLocal 中?
将ThreadLocalMap定义在Thread类内部看起来更符合逻辑,但是ThreadLocalMap并不需要Thread对象来操作,所以定义在Thread类内只会增加一些不必要的开销。定义在ThreadLocal类中的原因是ThreadLocal类负责ThreadLocalMap的创建,仅当线程中设置第一个ThreadLocal时,才为当前线程创建ThreadLocalMap,之后所有其他ThreadLocal变量将使用一个ThreadLocalMap。
总的来说就是,ThreadLocalMap不是必需品,定义在Thread中增加了成本,定义在ThreadLocal中按需创建。
6.2 ThreadLocalMap 中的key 为什么要使用弱引用:
ThreadLocalMap 中的key 使用弱引用是为了避免内存泄露:
ublic class Test {
public static void main(String[] args) {
ThreadLocal threadLocal = new ThreadLocal();
threadLocal.set(new Object());
threadLocal = null;
}
创建一个ThreadLocal对象,并设置一个Object对象,然后将其置空,如果key 不是弱引用的话,ThreadLocalMap中的key 就会有一个强引用指向ThreadLocal,如果不调用ThreadLocal 的remove()方法,GC 的时候发现key一直有强引用存在从而无法访问,但时间上改ThreadLocal 程序已经使用完了,永远也不可能在被程序用到,但是它依然还在占用空间;
当Key是弱引用时,threadLocal由于外部没有强引用了,GC可以将其回收,ThreadLocal通过key.get()==null可以判断Key已经被回收了,当前Entry是一个废弃的过期节点,因此ThreadLocal可以自发的清理这些过期节点,来避免「内存泄漏」。
6.3 既然每个Thread 都维护自己的变量数据为什么不直接使用 object:value 作为属性,而是要使用ThreadLocalMap 存储?
使用ThreadLocalMap 是因为一个线程中我们可以填充多个ThreadLocal ,如:
public class ThreadLocalTest {
final static ThreadLocal<String> threadLocal1 = new ThreadLocal<>();
final static ThreadLocal<String> threadLocal2 = new ThreadLocal<>();
final static ThreadLocal<String> threadLocal3 = new ThreadLocal<>();
public static void main(String[] args) throws NoSuchFieldException {
Thread t = Thread.currentThread();
threadLocal1.set("张三");
threadLocal2.set("李四");
threadLocal3.set("王五");
Class c = t.getClass();
Field field = c.getDeclaredField("threadLocals");
field.setAccessible(true);
String fieldName = field.getName();
Object fieldValue = null;
try {
fieldValue = field.get(t);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
threadLocal1.remove();
threadLocal2.remove();
threadLocal3.remove();
try {
fieldValue = field.get(t);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
int a =10;
}
}
在进行remove();之前我们可以看到ThreadLocalMap 存储了张三,李四 和王五:
在进行 remove();之后可以发现 ThreadLocalMap 中没有了张三,李四 和王五:
6.4 线性探索:
解决ThreadLocal ,hash冲突的一种策略,虽然ThreadLocal使用了很牛逼的办法来生成hashcode,但是还是不可避免会产生hash碰撞,当出现碰撞时,就找到当前ThreadLocal 的下标并向后探索进行插入或者查找:
set:
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
map.set(this, value);:
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();
}
通过ThreadLocal 获取hashcode并获取下标,当 k == null 时 replaceStaleEntry ;当key != null 并且 k != key 则向后遍历;如果循环结束都没有插入,则 new Entry(key, value) 并放入到 i 的下标处;
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);
}
在第二个for 循环向后遍历时,如果发现 entry中key 和 ThreadLocal key 相同,则直接设置value 并将该位置的entry 与 staleSlot的entry进行替换;
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.Entry e = map.getEntry(this):
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);
}
如果通过下标发现e为null 或者key 不相同 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;
}
通过for 循环向后遍历,如果发现key 相同则直接返回;
参考:
ThreadLocal为什么会导致内存泄漏?