Netty 源码之 FastThreadLocal

近期由于浏览Netty 的源码,想把Netty中一些编程技巧与优化的东西记录下来!来了解Netty中一些编程思想!

FastThreadLocal:对JDK中ThreadLocal进行优化,由于ThreadLocal底层存储数据是一个ThreadLocalMap 结构,是一个数组结构,通过threadLocalHashCode查找在数组中的元素Entry, 当hash冲突时,继续向前检测查找, 所以当Hash冲突时,检索的效率就会降低,具体可查看java.lang.ThreadLocal#getjava.lang.ThreadLocal.ThreadLocalMap#getEntry 方法。而FastThreadLocal则正是处理了这个问题,使其时间复杂度一直为O(1)

看一下 FastThredLocal 的系主要代码:

class UnpaddedInternalThreadLocalMap {
   //对非FastThreadLocalThread线程做兼容,从中获取InternalThreadLocalMap
   static final ThreadLocal slowThreadLocalMap = new ThreadLocal();
   //从此变量中获取线程存储数据的index
   static final AtomicInteger nextIndex = new AtomicInteger();
   //数据存储的数组
   Object[] indexedVariables;
} 
public final class InternalThreadLocalMap extends UnpaddedInternalThreadLocalMap {
    ....
    public static InternalThreadLocalMap get() {
        Thread thread = Thread.currentThread();
        //根据是否是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 slowThreadLocalMap = UnpaddedInternalThreadLocalMap.slowThreadLocalMap;
        InternalThreadLocalMap ret = slowThreadLocalMap.get();
        if (ret == null) {
            ret = new InternalThreadLocalMap();
            slowThreadLocalMap.set(ret);
        }
        return ret;
    }
   ...
}
    public class FastThreadLocal {
        ...
        private final int index;
        public FastThreadLocal() {
            index = InternalThreadLocalMap.nextVariableIndex();
        }

        //获取ThreadLocal当前线程对应保存的数据, 与ThreadLocal中的get()功能相同
        public final V get() {
            //InternalThreadLocalMap.get()获取InternalThreadLocalMap实例,从InternalThreadLocalMap中获取存储的数据
            return get(InternalThreadLocalMap.get());
        }

       public final V get(InternalThreadLocalMap threadLocalMap) {
            //根据index获取保存在UnpaddedInternalThreadLocalMap 中Object[] indexedVariables的值
            Object v = threadLocalMap.indexedVariable(index);
            if (v != InternalThreadLocalMap.UNSET) {
                return (V) v;
            }
            //为NULL 则调用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:当FastThreadLocalThreadFastThreadLocal中获取数据时,每一个FastThreadLocal都有一个index属性,此属性InternalThreadLocalMap.nextVariableIndex();赋值,保证了index的唯一性。index表示UnpaddedInternalThreadLocalMap属性indexedVariables数组的下标, 从而获取到保存在indexedVariables中的数据
3:如果获取的数据为null, 则调用FastThreadLocalinitialize()进行一个数据的初始化操作

你可能感兴趣的:(Netty 源码之 FastThreadLocal)