FastThreadLocal源码分析
public class FastThreadLocal<V> {
//这个值是FastThreadLocal的静态变量,所有的FastThreadLocal都用这个值,它是唯一的。
//在InternalThreadLocalMap有一个Object类型的数组indexedVariables
//那么indexedVariables[variablesToRemoveIndex] = new HashSet<FastThreadLocal>()。
//数组的variablesToRemoveIndex位置放一个SET,里面存储了全部的FastThreadLocal实例。
//indexedVariables[variablesToRemoveIndex] = HashSet[FastThreadLocal1,FastThreadLoca2,FastThreadLocalN]
private static final int variablesToRemoveIndex = InternalThreadLocalMap.nextVariableIndex();
public static void removeAll() {
//获取线程内的InternalThreadLocalMap对象,如果存在就返回,不存在返回null
InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.getIfSet();
if (threadLocalMap == null) {
//当前线程不存在InternalThreadLocalMap,则无需删除操作
return;
}
try {
//根据variablesToRemoveIndex下标获取InternalThreadLocalMap内部indexedVariables素组中的对象
Object v = threadLocalMap.indexedVariable(variablesToRemoveIndex);
//如果存在它一定是Set<FastThreadLocal<?>类型的对象,因为就这么设置的。
if (v != null && v != InternalThreadLocalMap.UNSET) {
//强制类型转换Set集合
Set<FastThreadLocal<?>> variablesToRemove = (Set<FastThreadLocal<?>>) v;
//转换为数组
FastThreadLocal<?>[] variablesToRemoveArray =
variablesToRemove.toArray(new FastThreadLocal[0]);
//循环调用FastThreadLocal.remove(传入线程内部InternalThreadLocalMap)
for (FastThreadLocal<?> tlv: variablesToRemoveArray) {
tlv.remove(threadLocalMap);
}
}
} finally {
//移除当前线程关联的InternalThreadLocalMap对象
InternalThreadLocalMap.remove();
}
}
//返回线程绑定的InternalThreadLocalMap对象内部一共存储了多少个元素
public static int size() {
InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.getIfSet();
if (threadLocalMap == null) {
return 0;
} else {
return threadLocalMap.size();
}
}
//移除普通线程的InternalThreadLocalMap对象,如果线程是FastThreadLocalThread则不移除。
public static void destroy() {
InternalThreadLocalMap.destroy();
}
private static void addToVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) {
//通过variablesToRemoveIndex的固定值,获取threadLocalMap成员变量indexedVariables[variablesToRemoveIndex]位置的对象
Object v = threadLocalMap.indexedVariable(variablesToRemoveIndex);
Set<FastThreadLocal<?>> variablesToRemove;
//如果返回空则创建一个set数据结构
if (v == InternalThreadLocalMap.UNSET || v == null) {
variablesToRemove = Collections.newSetFromMap(new IdentityHashMap<FastThreadLocal<?>, Boolean>());
//threadLocalMap.indexedVariables[variablesToRemoveIndex]= hashset对象
threadLocalMap.setIndexedVariable(variablesToRemoveIndex, variablesToRemove);
} else {
//如果存在set则赋值给variablesToRemove
variablesToRemove = (Set<FastThreadLocal<?>>) v;
}
//把当前对象加入到set当中去
//此时的数据结构为 :
//当前线程.InternalThreadLocalMap.indexedVariables[variablesToRemoveIndex]=hashset(FastThreadLocal对象1,
//FastThreadLocal对象2,FastThreadLocal对象3,当前对象this);
variablesToRemove.add(variable);
}
//threadLocalMap.indexedVariables[variablesToRemoveIndex]内的Set<FastThreadLocal<?>>数据结构中移除自己
private static void removeFromVariablesToRemove(
InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) {
//根据variablesToRemoveIndex拿出threadLocalMap的indexedVariables数组中的Set<FastThreadLocal<?>>
Object v = threadLocalMap.indexedVariable(variablesToRemoveIndex);
//如果返回值为null则结束
if (v == InternalThreadLocalMap.UNSET || v == null) {
return;
}
//转换为Set结构,移除自己this
@SuppressWarnings("unchecked")
Set<FastThreadLocal<?>> variablesToRemove = (Set<FastThreadLocal<?>>) v;
variablesToRemove.remove(variable);
}
//成员变量,通过InternalThreadLocalMap.nextVariableIndex()获取
//每个FastThreadLocal的对象,index值都会++保持唯一性
private final int index;
public FastThreadLocal() {
//获取index的值,每个FastThreadLocal对象index的值都会递增
index = InternalThreadLocalMap.nextVariableIndex();
}
//从当前线程的InternalThreadLocalMap中获取对象
public final V get() {
//在当前线程中获取InternalThreadLocalMap,没有则创建新的
InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.get();
//获取成员变量index位置的值
Object v = threadLocalMap.indexedVariable(index);
//如果值不是初始状态则返回v
if (v != InternalThreadLocalMap.UNSET) {
return (V) v;
}
//调用子类方法初始化value,并且写入到index位置
V value = initialize(threadLocalMap);
//标记位置
registerCleaner(threadLocalMap);
return value;
}
private void registerCleaner(final InternalThreadLocalMap threadLocalMap) {
//拿到当前线程
Thread current = Thread.currentThread();
//FastThreadLocalThread.willCleanupFastThreadLocals(current)
//如果线程是FastThreadLocalThread,则判断线程是否自动清理FastThreadLocal.removeAll();
//threadLocalMap.isCleanerFlagSet(index) 或者已经设置了标记位置
if (FastThreadLocalThread.willCleanupFastThreadLocals(current) || threadLocalMap.isCleanerFlagSet(index)) {
return;
}
//标记当前index
threadLocalMap.setCleanerFlag(index);
}
//获取对象
public final V get(InternalThreadLocalMap threadLocalMap) {
//获取threadLocalMap的indexedVariables[index]的对象
Object v = threadLocalMap.indexedVariable(index);
//如果不等于UNSET则返回
if (v != InternalThreadLocalMap.UNSET) {
return (V) v;
}
//否则初始化并设置对象
return initialize(threadLocalMap);
}
//子类初始化value,并把其写入到index位置
private V initialize(InternalThreadLocalMap threadLocalMap) {
V v = null;
try {
//调用子类方法获取对象V
v = initialValue();
} catch (Exception e) {
PlatformDependent.throwException(e);
}
//把V设置到threadLocalMap的indexedVariables[index]位置
threadLocalMap.setIndexedVariable(index, v);
//把自己加入到threadLocalMap.indexedVariables[variablesToRemoveIndex]=hashSet(this)当中去
addToVariablesToRemove(threadLocalMap, this);
return v;
}
//往当前线程的threadLocalMap的indexedVariables[index]位置设置对象
public final void set(V value) {
if (value != InternalThreadLocalMap.UNSET) {
InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.get();
if (setKnownNotUnset(threadLocalMap, value)) {
//设置标记位
registerCleaner(threadLocalMap);
}
} else {
remove();
}
}
//往threadLocalMap的indexedVariables[index]位置设置对象
public final void set(InternalThreadLocalMap threadLocalMap, V value) {
//如果V不等于UNSET
if (value != InternalThreadLocalMap.UNSET) {
setKnownNotUnset(threadLocalMap, value);
} else {
//否则移除对象
remove(threadLocalMap);
}
}
private boolean setKnownNotUnset(InternalThreadLocalMap threadLocalMap, V value) {
//往threadLocalMap的indexedVariables[index]= value 设置对象
if (threadLocalMap.setIndexedVariable(index, value)) {
//把当前对象加入到【threadLocalMap.indexedVariables[variablesToRemoveIndex]=set】 set数据结构中
addToVariablesToRemove(threadLocalMap, this);
return true;
}
return false;
}
//判断当前对象是否往threadLocalMap的indexedVariables数组中设置了值
public final boolean isSet() {
return isSet(InternalThreadLocalMap.getIfSet());
}
//判断当前对象是否往threadLocalMap的indexedVariables数组中设置了值
public final boolean isSet(InternalThreadLocalMap threadLocalMap) {
return threadLocalMap != null && threadLocalMap.isIndexedVariableSet(index);
}
public final void remove() {
remove(InternalThreadLocalMap.getIfSet());
}
//传入threadLocalMap对象
public final void remove(InternalThreadLocalMap threadLocalMap) {
//如果threadLocalMap == null则返回
if (threadLocalMap == null) {
return;
}
//通过当前对象的index,在threadLocalMap的indexedVariables数组中清除对象
Object v = threadLocalMap.removeIndexedVariable(index);
//在threadLocalMap的数组中indexedVariables[variablesToRemoveIndex]内的Set<FastThreadLocal<?>>数据结构中移除自己
removeFromVariablesToRemove(threadLocalMap, this);
//如果V不等于UNSET则出发一个事件
if (v != InternalThreadLocalMap.UNSET) {
try {
onRemoval((V) v);
} catch (Exception e) {
PlatformDependent.throwException(e);
}
}
}
//子类重写
protected V initialValue() throws Exception {
return null;
}
//子类重写
protected void onRemoval(@SuppressWarnings("UnusedParameters") V value) throws Exception { }
}