近期由于瀏覽Netty 的源碼,想把Netty中一些編程技巧與優(yōu)化的東西記錄下來!來了解Netty中一些編程思想莱革!
FastThreadLocal:對JDK中ThreadLocal進行優(yōu)化,由于ThreadLocal底層存儲數(shù)據(jù)是一個ThreadLocalMap 結(jié)構(gòu)讹开,是一個數(shù)組結(jié)構(gòu)盅视,通過threadLocalHashCode查找在數(shù)組中的元素Entry, 當hash沖突時,繼續(xù)向前檢測查找, 所以當Hash沖突時旦万,檢索的效率就會降低闹击,具體可查看java.lang.ThreadLocal#get
與 java.lang.ThreadLocal.ThreadLocalMap#getEntry
方法。而FastThreadLocal則正是處理了這個問題成艘,使其時間復(fù)雜度一直為O(1)
看一下 FastThredLocal 的系主要代碼:
class UnpaddedInternalThreadLocalMap {
//對非FastThreadLocalThread線程做兼容赏半,從中獲取InternalThreadLocalMap
static final ThreadLocal<InternalThreadLocalMap> slowThreadLocalMap = new ThreadLocal<InternalThreadLocalMap>();
//從此變量中獲取線程存儲數(shù)據(jù)的index
static final AtomicInteger nextIndex = new AtomicInteger();
//數(shù)據(jù)存儲的數(shù)組
Object[] indexedVariables;
}
public final class InternalThreadLocalMap extends UnpaddedInternalThreadLocalMap {
....
public static InternalThreadLocalMap get() {
Thread thread = Thread.currentThread();
//根據(jù)是否是FastThreadLocalThread 兩種獲取InternalThreadLocalMap 的方式
if (thread instanceof FastThreadLocalThread) {
return fastGet((FastThreadLocalThread) thread);
} else {
return slowGet();
}
}
//對于FastThreadLocalThread 線程,都會有一個InternalThreadLocalMap的引用淆两,直接獲取
private static InternalThreadLocalMap fastGet(FastThreadLocalThread thread) {
InternalThreadLocalMap threadLocalMap = thread.threadLocalMap();
if (threadLocalMap == null) {
thread.setThreadLocalMap(threadLocalMap = new InternalThreadLocalMap());
}
return threadLocalMap;
}
//對于非FastThreadLocalThread 線程
private static InternalThreadLocalMap slowGet() {
ThreadLocal<InternalThreadLocalMap> slowThreadLocalMap = UnpaddedInternalThreadLocalMap.slowThreadLocalMap;
InternalThreadLocalMap ret = slowThreadLocalMap.get();
if (ret == null) {
ret = new InternalThreadLocalMap();
slowThreadLocalMap.set(ret);
}
return ret;
}
...
}
public class FastThreadLocal<V> {
...
private final int index;
public FastThreadLocal() {
index = InternalThreadLocalMap.nextVariableIndex();
}
//獲取ThreadLocal當前線程對應(yīng)保存的數(shù)據(jù)断箫, 與ThreadLocal中的get()功能相同
public final V get() {
//InternalThreadLocalMap.get()獲取InternalThreadLocalMap實例,從InternalThreadLocalMap中獲取存儲的數(shù)據(jù)
return get(InternalThreadLocalMap.get());
}
public final V get(InternalThreadLocalMap threadLocalMap) {
//根據(jù)index獲取保存在UnpaddedInternalThreadLocalMap 中Object[] indexedVariables的值
Object v = threadLocalMap.indexedVariable(index);
if (v != InternalThreadLocalMap.UNSET) {
return (V) v;
}
//為NULL 則調(diào)用initialize(...)方法
return initialize(threadLocalMap);
}
private V initialize(InternalThreadLocalMap threadLocalMap) {
V v = null;
try {
v = initialValue();
} catch (Exception e) {
PlatformDependent.throwException(e);
}
threadLocalMap.setIndexedVariable(index, v);
addToVariablesToRemove(threadLocalMap, this);
return v;
}
...
}
從上面的代碼可以看出:
1:每一個FastThreadLocalThread都會有一個對InternalThreadLocalMap 的獲取秋冰,方法thread.threadLocalMap()
, 非FastThreadLocalThread
直接從UnpaddedInternalThreadLocalMap.slowThreadLocalMap
中獲取仲义。得到InternalThreadLocalMap
.
2:當FastThreadLocalThread
從FastThreadLocal
中獲取數(shù)據(jù)時,每一個FastThreadLocal都有一個index屬性剑勾,此屬性InternalThreadLocalMap.nextVariableIndex();
賦值埃撵,保證了index的唯一性。index表示UnpaddedInternalThreadLocalMap
屬性indexedVariables
數(shù)組的下標, 從而獲取到保存在indexedVariables
中的數(shù)據(jù)
3:如果獲取的數(shù)據(jù)為null
, 則調(diào)用FastThreadLocal
的initialize()
進行一個數(shù)據(jù)的初始化操作