Home / Class/ ThreadExecutorMap Class — netty Architecture

ThreadExecutorMap Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  aa0a8a62_b920_1f57_8e16_e9fe43f5f5bc["ThreadExecutorMap"]
  4e2525e0_769d_1e6c_219f_71e1127e9433["ThreadExecutorMap.java"]
  aa0a8a62_b920_1f57_8e16_e9fe43f5f5bc -->|defined in| 4e2525e0_769d_1e6c_219f_71e1127e9433
  91526484_8c07_0af7_7536_453f65896dd9["ThreadExecutorMap()"]
  aa0a8a62_b920_1f57_8e16_e9fe43f5f5bc -->|method| 91526484_8c07_0af7_7536_453f65896dd9
  a744dda6_b8eb_56b8_dcae_4ebc03e2d50f["EventExecutor()"]
  aa0a8a62_b920_1f57_8e16_e9fe43f5f5bc -->|method| a744dda6_b8eb_56b8_dcae_4ebc03e2d50f
  b1fca606_889a_bdb0_2d02_ba42eac1e30b["Executor()"]
  aa0a8a62_b920_1f57_8e16_e9fe43f5f5bc -->|method| b1fca606_889a_bdb0_2d02_ba42eac1e30b
  69d17fc6_2505_5c09_f459_56a5f5d083c9["Runnable()"]
  aa0a8a62_b920_1f57_8e16_e9fe43f5f5bc -->|method| 69d17fc6_2505_5c09_f459_56a5f5d083c9
  22050cbc_938c_0bf5_d249_fef9333178ec["ThreadFactory()"]
  aa0a8a62_b920_1f57_8e16_e9fe43f5f5bc -->|method| 22050cbc_938c_0bf5_d249_fef9333178ec

Relationship Graph

Source Code

common/src/main/java/io/netty/util/internal/ThreadExecutorMap.java lines 27–96

public final class ThreadExecutorMap {

    private static final FastThreadLocal<EventExecutor> mappings = new FastThreadLocal<EventExecutor>();

    private ThreadExecutorMap() { }

    /**
     * Returns the current {@link EventExecutor} that uses the {@link Thread}, or {@code null} if none / unknown.
     */
    public static EventExecutor currentExecutor() {
        return mappings.get();
    }

    /**
     * Set the current {@link EventExecutor} that is used by the {@link Thread}.
     */
    public static EventExecutor setCurrentExecutor(EventExecutor executor) {
        return mappings.getAndSet(executor);
    }

    /**
     * Decorate the given {@link Executor} and ensure {@link #currentExecutor()} will return {@code eventExecutor}
     * when called from within the {@link Runnable} during execution.
     */
    public static Executor apply(final Executor executor, final EventExecutor eventExecutor) {
        ObjectUtil.checkNotNull(executor, "executor");
        ObjectUtil.checkNotNull(eventExecutor, "eventExecutor");
        return new Executor() {
            @Override
            public void execute(final Runnable command) {
                executor.execute(apply(command, eventExecutor));
            }
        };
    }

    /**
     * Decorate the given {@link Runnable} and ensure {@link #currentExecutor()} will return {@code eventExecutor}
     * when called from within the {@link Runnable} during execution.
     */
    public static Runnable apply(final Runnable command, final EventExecutor eventExecutor) {
        ObjectUtil.checkNotNull(command, "command");
        ObjectUtil.checkNotNull(eventExecutor, "eventExecutor");
        return new Runnable() {
            @Override
            public void run() {
                EventExecutor old = setCurrentExecutor(eventExecutor);
                try {
                    command.run();
                } finally {
                    setCurrentExecutor(old);
                }
            }
        };
    }

    /**
     * Decorate the given {@link ThreadFactory} and ensure {@link #currentExecutor()} will return {@code eventExecutor}
     * when called from within the {@link Runnable} during execution.
     */
    public static ThreadFactory apply(final ThreadFactory threadFactory, final EventExecutor eventExecutor) {
        ObjectUtil.checkNotNull(threadFactory, "threadFactory");
        ObjectUtil.checkNotNull(eventExecutor, "eventExecutor");
        return new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return threadFactory.newThread(apply(r, eventExecutor));
            }
        };
    }
}

Frequently Asked Questions

What is the ThreadExecutorMap class?
ThreadExecutorMap is a class in the netty codebase, defined in common/src/main/java/io/netty/util/internal/ThreadExecutorMap.java.
Where is ThreadExecutorMap defined?
ThreadExecutorMap is defined in common/src/main/java/io/netty/util/internal/ThreadExecutorMap.java at line 27.

Analyze Your Own Codebase

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

Try Supermodel Free