Netty之FastThreadLocal

Netty的FastThreadLocal优化了ThreadLocal的查找效率,避免了线性扫描的过程。它创建了自己的FastThreadLocalThread类,包含专属的ThreadLocalMap,并通过静态成员简化removeAll操作。使用bitSet的cleanerFlag记录清除位数,后台守护线程负责清理,初始化时直接填充32个对象以提高性能。
摘要由CSDN通过智能技术生成

FastThreadLocal,看名字就可以知道,netty君让其优化了。我们先来看看ThreadLocal哪里慢了需要fast,上一篇分析我们知道,Thread内有一个ThreadLocalMap的成员,该成员相当于一个map(数组+线性扫描),该map以ThreadLocal为key,若要定位到相应的value,需要两步。先是根据ThreadLocal的hashCode取余定位到数组的小标,因为是线性扫描,所以很有可能当前不是,需要往后遍历直到找到。而netty,使用一定的机制使其无需第二步操作。

因为ThreadLocal里面没有相应的接口,netty只能自己实现FastThreadLocalThread继承Thread在里面设有自己的ThreadLocalMap。

public class FastThreadLocalThread extends Thread {
    // This will be set to true if we have a chance to wrap the Runnable.
    private final boolean cleanupFastThreadLocals;

    private InternalThreadLocalMap threadLocalMap;
其中cleanupFastThreadLocals从注释中可以读出当封装runnable时候该值为true。
    private InternalThreadLocalMap() {
        super(newIndexedVariableTable());
    }

    private static Object[] newIndexedVariableTable() {
        Object[] array = new Object[32];
        Arrays.fill(array, UNSET);
        return array;
    }
从InternalThreadLocalMap的构造可以看到,InternalThreadLocalMap也采用了数组形式存储。
    UnpaddedInternalThreadLocalMap(Object[] indexedVariables) {
        this.indexedVariables = indexedVariables;
    }
我们再看看其父类UnpaddedInternalThreadLocalMap的成员
    static final ThreadLocal<InternalThreadLocalMap> slowThreadLocalMap = new ThreadLocal<InternalThreadLocalMap>();
    static final AtomicInteger nextIndex = new AtomicInteger();

    /** Used by {@link FastThreadLocal} */
    Object[] indexedVariables;

    // Core thread-locals
    int futureListenerStackDepth;
    int localChannelReaderStackDepth;
    Map<Class<?>, Boolean> handlerSharableCache;
    IntegerHolder counterHashCode;
    ThreadLocalRandom random;
    Map<Class<?>, TypeParameterMatcher> typeParameterMatcherGetCache;
    Map<Class<?>, Map<String, TypeParameterMatcher>> typeParameterMatcherFindCache;

    // String-related thread-locals
    StringBuilder stringBuilder;
    Map<Charset, CharsetEncoder> charsetEncoderCache;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值