目录
ThreadLocal的作用
比如在进行参数跨越多层传递的时候,使用ThreadLocal可以让代码更加简洁。进行事务处理,可以用于存储线程事务信息。又或者线程的数据隔离上下文Context。
本文的源码分析主要从以下方法去深入:
public void set(T value)
public T get()
public void remove()
Set()方法
先从thread中取得一个map,Thread.currentThread()获取的是当前线程的Thread对象,后面的操作针对这个ThreadLocalMap对象。
当map为空时进行初始化,初始化会指定存储entry的数组的大小。threshold用于Rehash,当数组用了2/3的时候会进行扩容。
public void set(T value) {
Thread t = Thread.currentThread();
//从当前线程中获取ThreadLocalMap对象
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);
}
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);
}
/**
* Set the resize threshold to maintain at worst a 2/3 load factor.
*/
private void setThreshold(int len) {
threshold = len * 2 / 3;
}
/**
* The initial capacity -- MUST be a power of two.
*/
private static final int INITIAL_CAPACITY = 16;
在Thead类中包含了该字段。
public class Thread implements Runnable {
//省略...
/* ThreadLocal values pertaining to this thread. This map is maintained
* by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;
//省略...
}
继续往下看。不难看出tab就是类似于HashMap里面的数组结构了,Hash函数key.threadLocalHashCode & (len-1)用于定位插槽位置。entry继承了WeakReference,并且调用了super(k);方法让k变成弱引用。弱引用的对象在gc的时候会被回收。
从以下代码不难看出底层没有使用链表法来解决hash冲突,而是不断调用nextIndex寻找下一个位置去寻找空的位置,这种Hash冲突称为线性探测法。这里key的比较直接按照地址来(“==”),因为这里的key是threadlocal对象,同一对象的堆内存地址是不变的。
cleanSomeSlot是清除一些threadlocal为null的插槽。
private void set(ThreadLocal<?> key, Object value) {
Entry[] tab = table;
int len = tab.length;
//获取Hash值定位的首个位置
int i = key.threadLocalHashCode & (len-1);
//不断nextIndex,迭代遍历
for (Entry e = tab[i];
e != null;
e = tab[i = nextIndex(i, len)]) {
//从Entry里面获取ThreadLocal对象
ThreadLocal<?> k = e.get();
//判断堆内存地址是否相等,这里并不像HashMap一样还需要调用equals方法
if (k == key) {
e.value = value;
return;
}
// threadlocal对象被回收或者空插槽直接赋值
if (k == null) {
replaceStaleEntry(key, value, i);
return;
}
}
//e == null
tab[i] = new Entry(key, value);
int sz = ++size;
//数量达到2/3时,rehash
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
private static int nextIndex(int i, int len) {
return ((i + 1 < len) ? i + 1 : 0);
}
private boolean cleanSomeSlots(int i, int n) {
boolean removed = false;
Entry[] tab = table;
int len = tab.length;
do {
i = nextIndex(i, len);
Entry e = tab[i];
if (e != null && e.get() == null) {
n = len;
removed = true;
i = expungeStaleEntry(i);
}
} while ( (n >>>= 1) != 0);
return removed;
}
threadLocalHashCode是一个在ThreadLocal初始化就被指定的差值,初始值利用全局的AtomicInteger的getAndAdd完成,AtomicInteger提供了自增以及线程安全的API。
public class ThreadLocal<T> {
private final int threadLocalHashCode = nextHashCode();
/**
* The next hash code to be given out. Updated atomically. Starts at
* zero.
*/
private static AtomicInteger nextHashCode =
new AtomicInteger();
}
/**
* Returns the next hash code.
*/
private static int nextHashCode() {
return nextHashCode.getAndAdd(HASH_INCREMENT);
}
总结:TheadLocal底层通过Thead.currentThread获取每个thread对象的ThreadLocalMap,Set(T)方法会将Threadlocal对象本身作为key值,T作为value存入map当中。ThreadLocalMap的插槽继承了WeakReference,并调用了父类的构造方法让key变成弱引用。ThreadLocalMap底层实现为Entry数组+线性探测法,负载因子为2/3。
Get()方法
首先进入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();
}
getEntry先利用Hash值找到首个插槽,如果插槽的key的对象地址与需要的key的地址相同则直接返回,否则利用线性探测法去寻找下一个地址。
private Entry getEntry(ThreadLocal<?> key) {
//计算Hash值
int i = key.threadLocalHashCode & (table.length - 1);
//获取首个entry
Entry e = table[i];
if (e != null && e.get() == key)
//没有发生Hash冲突
return e;
else
//发生了Hash冲突,线性探测法寻找key
return getEntryAfterMiss(key, i, e);
}
getEntryAfterMiss主要是线性探测法的实现。
private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) {
Entry[] tab = table;
int len = tab.length;
//循环找符合的Entry
while (e != null) {
ThreadLocal<?> k = e.get();
if (k == key)
return e;
if (k == null)
//k为null说明了此时的Threadlocal没有强引用了,已经被gc回收
expungeStaleEntry(i);
else
i = nextIndex(i, len);
e = tab[i];
}
return null;
}
expunge Stale Entry意为删除过时的条目,通过rehash来删除。
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;
}
如果没有Get到值就先set初始化值再返回。
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;
}
Remove()方法
从threadLocalMap中删除key(当前的threadlocal对象)
/**
* Removes the current thread's value for this thread-local
* variable. If this thread-local variable is subsequently
* {@linkplain #get read} by the current thread, its value will be
* reinitialized by invoking its {@link #initialValue} method,
* unless its value is {@linkplain #set set} by the current thread
* in the interim. This may result in multiple invocations of the
* {@code initialValue} method in the current thread.
*
* @since 1.5
*/
public void remove() {
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null) {
m.remove(this);
}
}
总结一下:ThreadLocal在每个Thread对象中都存储了一个ThreadLocalMap对象,每次都会拿到这个Map以取这个线程的ThreadLocal对象为key。Set和Get方法以及Remove方法都针对这个数据结构进行操作。ThreadLocalMap底层是数组+线性探测法。
父子线程传递问题
先看这样一个问题,在父线程new一个子线程的时候,子线程无法获取到父线程的threadLocalMap对象。
ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
threadLocal.set(1);
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(threadLocal.get());
}
}).start();
输出结果:null
针对以上问题JDK提供了解决方案:InheritableThreadLocal
ThreadLocal<Integer> threadLocal = new InheritableThreadLocal<>();
threadLocal.set(1);
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(threadLocal.get());
}
}).start();
输出结果:1
InheritableThreadLocal
InheritableThreadLocal继承了ThreadLocal,重写了以下三个方法
//java.lang.InheritThreadLocals
public class InheritableThreadLocal<T> extends ThreadLocal<T> {
protected T childValue(T parentValue) {
return parentValue;
}
ThreadLocalMap getMap(Thread t) {
return t.inheritableThreadLocals;
}
void createMap(Thread t, T firstValue) {
t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);
}
}
该字段在Thread类中,与threadLocals区别开。
//java.lang.Thread
/* ThreadLocal values pertaining to this thread. This map is maintained
* by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;
/*
* InheritableThreadLocal values pertaining to this thread. This map is
* maintained by the InheritableThreadLocal class.
*/
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
接下来看Thread类的构造方法:
//java.lang.Thread
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
private void init(ThreadGroup g, Runnable target, String name,
long stackSize) {
//inheritThreadLocals -> true
init(g, target, name, stackSize, null, true);
}
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals) {
this.name = name;
//父线程
Thread parent = currentThread();
//省略其他代码
//parent.inheritableThreadLocals != null 说明子线程需要父线程数据
if (inheritThreadLocals && parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
//省略其他代码
}
//java.lang.ThreadLocal
static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {
return new ThreadLocalMap(parentMap);
}
//数据拷贝
private ThreadLocalMap(ThreadLocalMap parentMap) {
Entry[] parentTable = parentMap.table;
int len = parentTable.length;
setThreshold(len);
table = new Entry[len];
for (int j = 0; j < len; j++) {
Entry e = parentTable[j];
if (e != null) {
@SuppressWarnings("unchecked")
ThreadLocal<Object> key = (ThreadLocal<Object>) e.get();
if (key != null) {
//childValue已经被重写,返回的就是e.value本身
Object value = key.childValue(e.value);
Entry c = new Entry(key, value);
int h = key.threadLocalHashCode & (len - 1);
while (table[h] != null)
h = nextIndex(h, len);
table[h] = c;
size++;
}
}
}
}
总结一下就是 InheritThreadLocals继承了ThreadLocal,并重写了三个方法;createMap、getMap、childValue
在new子线程的时候会进行数据的深拷贝,创建另外一个ThreadLocalMap副本。
问题再深入:线程池Submit问题
现在许多场景并不需要手动取new Thread线程,而是使用线程池对象,此时new Thread就不能够拷贝正确的父线程的数据了。
public static void main(String[] args) throws InterruptedException{
ThreadLocal<String > threadLocal = new InheritableThreadLocal<>();
threadLocal.set("main thread");
//核心线程数
int core = 1;
//使用默认的线程工厂
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(core, core, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
final Runnable task = () -> threadLocal.set("task thread");
poolExecutor.submit(task);
Thread.sleep(1000);
poolExecutor.submit(() -> {
System.out.println(threadLocal.get());
});
Thread.sleep(1000);
poolExecutor.shutdown();
}
从主线程提交任务,输出结果理应为:main thread
真实输出结果:task thread
因为线程池的线程创建是懒加载方式的,哪个线程在最开始提供了任务哪个线程就调用了new Thread,小于核心线程数的时候这些线程不会销毁,会被复用到其他任务上,这就导致了其他线程submit时,无法拷贝“父线程”的资源了。
//java.util.concurrent.ThreadPoolExecutor
//线程池初始化不会提供任何创建线程的逻辑
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
//继续来看submit方法
public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException();
RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask);
return ftask;
}
//工作小于核心线程就调用addWroker方法
public void execute(Runnable command) {
//省略
//工作线程小于核心线程数会走这里
if (workerCountOf(c) < corePoolSize) {
//添加工作线程并执行
if (addWorker(command, true))
return;
c = ctl.get();
}
//省略
}
//这里会调用创建线程的方法
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
//自旋
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
for (;;) {
int wc = workerCountOf(c);
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
if (compareAndIncrementWorkerCount(c))
break retry;
c = ctl.get(); // Re-read ctl
if (runStateOf(c) != rs)
continue retry;
// else CAS failed due to workerCount change; retry inner loop
}
}
boolean workerStarted = false;
boolean workerAdded = false;
Worker w = null;
try {
//主要关系线程创建逻辑
w = new Worker(firstTask);
final Thread t = w.thread;
//省略...
}
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
//这里我们使用的是默认的线程工厂DefaultThreadFactory
this.thread = getThreadFactory().newThread(this);
}
//java.util.concurrent.ThreadPoolExecutor:static class DefaultThreadFactory
public ThreadFactory getThreadFactory() {
return threadFactory;
}
//真正调用new Thread的地方,这里说明新线程由提交的线程创建的
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
阿里巴巴提供了一个开源的ThreadLocal,实现了这种需求。
public static void main(String[] args) throws InterruptedException{
ThreadLocal<String > threadLocal = new TransmittableThreadLocal<>();
threadLocal.set("main thread");
int core = 1;
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(core, core, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
final Runnable task = () -> threadLocal.set("task thread");
Runnable ttlRunnable = TtlRunnable.get(task);
poolExecutor.submit(ttlRunnable);
Thread.sleep(1000);
poolExecutor.submit(() -> {
System.out.println(threadLocal.get());
});
Thread.sleep(1000);
poolExecutor.shutdown();
}
输出结果:main thread
与输出结果一致,那他又是怎么实现的?
ThreadLocal<String > threadLocal = new TransmittableThreadLocal<>();
Runnable ttlRunnable = TtlRunnable.get(task);