ThreadLocal 工作机制
实现原理
public class Thread implements Runnable {
// ......
/* ThreadLocal values pertaining to this thread. This map is maintained
* by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;
// ......
}public class ThreadLocal<T> {
// ......
static class ThreadLocalMap {
static class Entry extends WeakReference<ThreadLocal<?>> {
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
private Entry[] table;
private void set(ThreadLocal<?> key, Object value) {
Entry[] tab = table;
// ......
tab[i] = new Entry(key, value);
// ......
}
// ......
}
// ......
}一图概览
内存泄漏
在父子线程之间传递数据
向可复用的线程传递数据
最后更新于