Home / Class/ FastThreadLocal Class — netty Architecture

FastThreadLocal Class — netty Architecture

Architecture documentation for the FastThreadLocal class in FastThreadLocal.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3["FastThreadLocal"]
  0bf56b8e_2e4a_3f63_6931_914327934433["FastThreadLocal.java"]
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3 -->|defined in| 0bf56b8e_2e4a_3f63_6931_914327934433
  52377f16_e357_e793_1546_383fbe5cdcf2["removeAll()"]
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3 -->|method| 52377f16_e357_e793_1546_383fbe5cdcf2
  b5aef822_0985_dee1_cfe6_bd30a9a88bbd["size()"]
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3 -->|method| b5aef822_0985_dee1_cfe6_bd30a9a88bbd
  a209d19f_cf57_a6ad_689a_b1b64f3de6ce["destroy()"]
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3 -->|method| a209d19f_cf57_a6ad_689a_b1b64f3de6ce
  4e211cb3_deeb_f3a6_e938_836baec61ba0["addToVariablesToRemove()"]
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3 -->|method| 4e211cb3_deeb_f3a6_e938_836baec61ba0
  ad42c8d2_ed18_9e7c_42e2_f00f51b8e0be["removeFromVariablesToRemove()"]
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3 -->|method| ad42c8d2_ed18_9e7c_42e2_f00f51b8e0be
  928d6c7a_37d6_bffa_fe37_36e96fa87364["FastThreadLocal()"]
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3 -->|method| 928d6c7a_37d6_bffa_fe37_36e96fa87364
  277e015e_1062_f2b5_7f17_dacb7cb7fec5["V()"]
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3 -->|method| 277e015e_1062_f2b5_7f17_dacb7cb7fec5
  a3e95267_be9b_0411_c0e9_02a84f57e196["set()"]
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3 -->|method| a3e95267_be9b_0411_c0e9_02a84f57e196
  f1fc2027_5e91_fc98_2fa2_d08b5a1b954e["isSet()"]
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3 -->|method| f1fc2027_5e91_fc98_2fa2_d08b5a1b954e
  5a30b348_866f_4505_5309_1f89c39f3785["remove()"]
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3 -->|method| 5a30b348_866f_4505_5309_1f89c39f3785
  09c6b172_96a0_2674_13aa_da5631603998["onRemoval()"]
  dc6344c3_40c4_25b8_2eda_8ec5bb8163b3 -->|method| 09c6b172_96a0_2674_13aa_da5631603998

Relationship Graph

Source Code

common/src/main/java/io/netty/util/concurrent/FastThreadLocal.java lines 47–308

public class FastThreadLocal<V> {

    /**
     * Removes all {@link FastThreadLocal} variables bound to the current thread.  This operation is useful when you
     * are in a container environment, and you don't want to leave the thread local variables in the threads you do not
     * manage.
     */
    public static void removeAll() {
        InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.getIfSet();
        if (threadLocalMap == null) {
            return;
        }

        try {
            Object v = threadLocalMap.indexedVariable(VARIABLES_TO_REMOVE_INDEX);
            if (v != null && v != InternalThreadLocalMap.UNSET) {
                @SuppressWarnings("unchecked")
                Set<FastThreadLocal<?>> variablesToRemove = (Set<FastThreadLocal<?>>) v;
                FastThreadLocal<?>[] variablesToRemoveArray =
                        variablesToRemove.toArray(new FastThreadLocal[0]);
                for (FastThreadLocal<?> tlv: variablesToRemoveArray) {
                    tlv.remove(threadLocalMap);
                }
            }
        } finally {
            InternalThreadLocalMap.remove();
        }
    }

    /**
     * Returns the number of thread local variables bound to the current thread.
     */
    public static int size() {
        InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.getIfSet();
        if (threadLocalMap == null) {
            return 0;
        } else {
            return threadLocalMap.size();
        }
    }

    /**
     * Destroys the data structure that keeps all {@link FastThreadLocal} variables accessed from
     * non-{@link FastThreadLocalThread}s.  This operation is useful when you are in a container environment, and you
     * do not want to leave the thread local variables in the threads you do not manage.  Call this method when your
     * application is being unloaded from the container.
     */
    public static void destroy() {
        InternalThreadLocalMap.destroy();
    }

    @SuppressWarnings("unchecked")
    private static void addToVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) {
        Object v = threadLocalMap.indexedVariable(VARIABLES_TO_REMOVE_INDEX);
        Set<FastThreadLocal<?>> variablesToRemove;
        if (v == InternalThreadLocalMap.UNSET || v == null) {
            variablesToRemove = Collections.newSetFromMap(new IdentityHashMap<FastThreadLocal<?>, Boolean>());
            threadLocalMap.setIndexedVariable(VARIABLES_TO_REMOVE_INDEX, variablesToRemove);
        } else {
            variablesToRemove = (Set<FastThreadLocal<?>>) v;
        }

        variablesToRemove.add(variable);
    }

    private static void removeFromVariablesToRemove(
            InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) {

        Object v = threadLocalMap.indexedVariable(VARIABLES_TO_REMOVE_INDEX);

        if (v == InternalThreadLocalMap.UNSET || v == null) {
            return;
        }

        @SuppressWarnings("unchecked")
        Set<FastThreadLocal<?>> variablesToRemove = (Set<FastThreadLocal<?>>) v;
        variablesToRemove.remove(variable);
    }

    private final int index;

Frequently Asked Questions

What is the FastThreadLocal class?
FastThreadLocal is a class in the netty codebase, defined in common/src/main/java/io/netty/util/concurrent/FastThreadLocal.java.
Where is FastThreadLocal defined?
FastThreadLocal is defined in common/src/main/java/io/netty/util/concurrent/FastThreadLocal.java at line 47.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free