Java Code Examples for io.netty.util.internal.InternalThreadLocalMap#setIndexedVariable()

The following examples show how to use io.netty.util.internal.InternalThreadLocalMap#setIndexedVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FastThreadLocal.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static void addToVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) {
    Object v = threadLocalMap.indexedVariable(variablesToRemoveIndex);
    Set<FastThreadLocal<?>> variablesToRemove;
    if (v == InternalThreadLocalMap.UNSET || v == null) {
        variablesToRemove = Collections.newSetFromMap(new IdentityHashMap<FastThreadLocal<?>, Boolean>());
        threadLocalMap.setIndexedVariable(variablesToRemoveIndex, variablesToRemove);
    } else {
        variablesToRemove = (Set<FastThreadLocal<?>>) v;
    }

    variablesToRemove.add(variable);
}
 
Example 2
Source File: FastThreadLocal.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void registerCleaner(final InternalThreadLocalMap threadLocalMap) {
        Thread current = Thread.currentThread();
        if (FastThreadLocalThread.willCleanupFastThreadLocals(current) ||
            threadLocalMap.indexedVariable(cleanerFlagIndex) != InternalThreadLocalMap.UNSET) {
            return;
        }
        // removeIndexedVariable(cleanerFlagIndex) isn't necessary because the finally cleanup is tied to the lifetime
        // of the thread, and this Object will be discarded if the associated thread is GCed.
        // removeIndexedVariable(cleanerFlagIndex)不是必需的,因为最终的清理与生命周期有关
//线程,如果关联的线程是GCed,这个对象将被丢弃。
        threadLocalMap.setIndexedVariable(cleanerFlagIndex, Boolean.TRUE);

        // We will need to ensure we will trigger remove(InternalThreadLocalMap) so everything will be released
        // and FastThreadLocal.onRemoval(...) will be called.
        //我们需要确保我们将触发remove(InternalThreadLocalMap),以便释放所有内容
//和fastthreadlocal.onremove(…)将被调用。
        ObjectCleaner.register(current, new Runnable() {
            @Override
            public void run() {
                remove(threadLocalMap);

                // It's fine to not call InternalThreadLocalMap.remove() here as this will only be triggered once
                // the Thread is collected by GC. In this case the ThreadLocal will be gone away already.在这里不调用InternalThreadLocalMap.remove()是可以的,因为它只会被触发一次
//线程被GC收集。在这种情况下,ThreadLocal已经消失了。
            }
        });
    }
 
Example 3
Source File: FastThreadLocal.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 4
Source File: FastThreadLocal.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
     * @return see {@link InternalThreadLocalMap#setIndexedVariable(int, Object)}.
     */
//
    private boolean setKnownNotUnset(InternalThreadLocalMap threadLocalMap, V value) {
        if (threadLocalMap.setIndexedVariable(index, value)) {
            addToVariablesToRemove(threadLocalMap, this);
            return true;
        }
        return false;
    }
 
Example 5
Source File: FastThreadLocal.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static void addToVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) {
    Object v = threadLocalMap.indexedVariable(variablesToRemoveIndex);
    Set<FastThreadLocal<?>> variablesToRemove;
    if (v == InternalThreadLocalMap.UNSET || v == null) {
        variablesToRemove = Collections.newSetFromMap(new IdentityHashMap<FastThreadLocal<?>, Boolean>());
        threadLocalMap.setIndexedVariable(variablesToRemoveIndex, variablesToRemove);
    } else {
        variablesToRemove = (Set<FastThreadLocal<?>>) v;
    }

    variablesToRemove.add(variable);
}
 
Example 6
Source File: FastThreadLocal.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: FastThreadLocal.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
/**
 * Set the value for the specified thread local map. The specified thread local map must be for the current thread.
 */
public final void set(InternalThreadLocalMap threadLocalMap, V value) {
    if (value != InternalThreadLocalMap.UNSET) {
        if (threadLocalMap.setIndexedVariable(index, value)) {
            addToVariablesToRemove(threadLocalMap, this);
        }
    } else {
        remove(threadLocalMap);
    }
}