import java.lang.ref.*;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
public class ThreadLocal {
private final int threadLocalHashCode = nextHashCode();
/**
* The next hash code to be given out. Updated atomically. Starts at
* zero.
*/
private static AtomicInteger nextHashCode =
new AtomicInteger();
private static final int HASH_INCREMENT = 0x61c88647;
private static int nextHashCode() {
return nextHashCode.getAndAdd(HASH_INCREMENT);
}
protected T initialValue() {
return null;
}
public static java.lang.ThreadLocal withInitial(Supplier extends S> supplier) {
return new java.lang.ThreadLocal.SuppliedThreadLocal<>(supplier);
}
public ThreadLocal() {
}
/**
* @return the current thread's value of this thread-local 返回当前线程对应此ThreadLocal的值
*/
public T get() {
//获取当前线程
Thread t = Thread.currentThread();
//获取此线程对象中维护的ThreadLocalMap对象
java.lang.ThreadLocal.ThreadLocalMap map = getMap(t);
//如果此map存在
if (map != null) {
//以当前的ThreadLocal为key,调用getEntry获取对应的存储实体e
java.lang.ThreadLocal.ThreadLocalMap.Entry e = map.getEntry(this);
//对e判空
if (e != null) {
//如果e不为空,则根据泛型强转值
@SuppressWarnings("unchecked")
T result = (T)e.value;
//返回该值
return result;
}
}
//初始化,两种情况执行该代码
//(1)、当前线程map不存在,即此线程没有维护的ThreadLocalMap
//(2)、map存在,e为空,即线程有维护的ThreadLocalMap但是此ThreadLocal没有关联的值
return setInitialValue();
}
//初始化,该方法是在ThreadLocal对象没有调用set(),而直接调用get()方法时调用,并且此方法只调用一次
private T setInitialValue() {
//调用initialValue获取初始化的值
//此方法可以被子类重写,如果不重写默认返回null
T value = initialValue();
//获取当前线程对象
Thread t = Thread.currentThread();
//获取该线程对象中维护的ThreadLocalMap对象
java.lang.ThreadLocal.ThreadLocalMap map = getMap(t);
//如果此线程对应map不为空
if (map != null)
//调用map并设置值
map.set(this, value);
else
//此线程没有ThreadLocalMap则调用createMap
createMap(t, value);
//返回value,如果不重写initialValue,则就是返回null
return value;
}
/**
* 设置当前对象对应的ThreadLocal的值 lcgset
* @param value 将要保存在当前线程对应的ThreadLocal的值
*/
public void set(T value) {
//获取当前线程对象
Thread t = Thread.currentThread();
//获取此线程中维护ThreadLocalMap对象
java.lang.ThreadLocal.ThreadLocalMap map = getMap(t);
//判断map对象是否为空
if (map != null)
//存在则调用map.set设置此实体entry
map.set(this, value);
else
//1)当前线程Thread不存在ThreadLocalMap对象
//2)调用createMap进行ThreadLocalMap对象初始化
createMap(t, value);
}
//删除当前线程中保存的ThreadLocal(this)对应得实体
public void remove() {
//根据当前线程获取map
java.lang.ThreadLocal.ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
//如果map不为空,则根据键(调用此方法的ThreadLocal对象)移除该元素
m.remove(this);
}
java.lang.ThreadLocal.ThreadLocalMap getMap(Thread t) {
//返回此线程t中的ThreadLocalMap
return t.threadLocals;
}
void createMap(Thread t, T firstValue) {
//创建一个ThreadLocalMap,把此ThreadLocal和value作为第一个entry存放至ThreadLocalMap中,
// 并把该对象,引用给此线程t中threadLocals 成员变量
t.threadLocals = new java.lang.ThreadLocal.ThreadLocalMap(this, firstValue);
}
static java.lang.ThreadLocal.ThreadLocalMap createInheritedMap(java.lang.ThreadLocal.ThreadLocalMap parentMap) {
return new java.lang.ThreadLocal.ThreadLocalMap(parentMap);
}
T childValue(T parentValue) {
throw new UnsupportedOperationException();
}
static final class SuppliedThreadLocal extends java.lang.ThreadLocal {
private final Supplier extends T> supplier;
SuppliedThreadLocal(Supplier extends T> supplier) {
this.supplier = Objects.requireNonNull(supplier);
}
@Override
protected T initialValue() {
return supplier.get();
}
}
static class ThreadLocalMap {
/**
* The entries in this hash map extend WeakReference, using
* its main ref field as the key (which is always a
* ThreadLocal object). Note that null keys (i.e. entry.get()
* == null) mean that the key is no longer referenced, so the
* entry can be expunged from table. Such entries are referred to
* as "stale entries" in the code that follows.
*/
static class Entry extends WeakReference> {
/** The value associated with this ThreadLocal. */
//与此ThreadLocal关联的值
Object value;
//Entry也是键值对结构,但它的键在构造方法中就限定死了,必须是ThreadLocal类型
Entry(java.lang.ThreadLocal> k, Object v) {
super(k);
value = v;
}
}
/**
* The initial capacity -- MUST be a power of two.
* 初始容量--必须是2的整数幂
*/
private static final int INITIAL_CAPACITY = 16;
/**存放数据table
* 表,根据需要调整大小
* The table, resized as necessary.
* table的长度也必须是2的整数幂
* table.length MUST always be a power of two.
*/
private java.lang.ThreadLocal.ThreadLocalMap.Entry[] table;
/**
* The number of entries in the table.
* table数据中entries的个数
*/
private int size = 0;
/**
* The next size value at which to resize.
* 要调整大小的下一个大小值
*/
private int threshold; // Default to 0
/**
* Set the resize threshold to maintain at worst a 2/3 load factor.
*/
private void setThreshold(int len) {
threshold = len * 2 / 3;
}
/**
* Increment i modulo len.
*/
private static int nextIndex(int i, int len) {
return ((i + 1 < len) ? i + 1 : 0);
}
/**
* Decrement i modulo len.
*/
private static int prevIndex(int i, int len) {
return ((i - 1 >= 0) ? i - 1 : len - 1);
}
/**
* Construct a new map initially containing (firstKey, firstValue).
* ThreadLocalMaps are constructed lazily, so we only create
* one when we have at least one entry to put in it.
*/
ThreadLocalMap(java.lang.ThreadLocal> firstKey, Object firstValue) {
table = new java.lang.ThreadLocal.ThreadLocalMap.Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new java.lang.ThreadLocal.ThreadLocalMap.Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY);
}
/**
* Construct a new map including all Inheritable ThreadLocals
* from given parent map. Called only by createInheritedMap.
*
* @param parentMap the map associated with parent thread.
*/
private ThreadLocalMap(java.lang.ThreadLocal.ThreadLocalMap parentMap) {
java.lang.ThreadLocal.ThreadLocalMap.Entry[] parentTable = parentMap.table;
int len = parentTable.length;
setThreshold(len);
table = new java.lang.ThreadLocal.ThreadLocalMap.Entry[len];
for (int j = 0; j < len; j++) {
java.lang.ThreadLocal.ThreadLocalMap.Entry e = parentTable[j];
if (e != null) {
@SuppressWarnings("unchecked")
java.lang.ThreadLocal