TtlExecutors.java 4.1 KB
Newer Older
1 2 3 4
package com.alibaba.ttl.threadpool;

import com.alibaba.ttl.TransmittableThreadLocal;

oldratlee's avatar
oldratlee 已提交
5
import javax.annotation.Nullable;
6 7 8 9 10
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;

/**
11
 * Factory Utils for getting TTL wrapper of jdk executors.
12 13
 *
 * @author Jerry Lee (oldratlee at gmail dot com)
oldratlee's avatar
oldratlee 已提交
14 15 16 17 18 19 20
 * @see java.util.concurrent.Executor
 * @see java.util.concurrent.ExecutorService
 * @see java.util.concurrent.ThreadPoolExecutor
 * @see java.util.concurrent.ScheduledThreadPoolExecutor
 * @see java.util.concurrent.Executors
 * @see java.util.concurrent.CompletionService
 * @see java.util.concurrent.ExecutorCompletionService
oldratlee's avatar
oldratlee 已提交
21
 * @since 0.9.0
22 23 24 25 26 27 28
 */
public final class TtlExecutors {
    /**
     * {@link TransmittableThreadLocal} Wrapper of {@link Executor},
     * transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable}
     * to the execution time of {@link Runnable}.
     */
oldratlee's avatar
oldratlee 已提交
29 30
    @Nullable
    public static Executor getTtlExecutor(@Nullable Executor executor) {
31 32 33 34 35 36 37 38 39 40 41
        if (null == executor || executor instanceof ExecutorTtlWrapper) {
            return executor;
        }
        return new ExecutorTtlWrapper(executor);
    }

    /**
     * {@link TransmittableThreadLocal} Wrapper of {@link ExecutorService},
     * transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable} or {@link java.util.concurrent.Callable}
     * to the execution time of {@link Runnable} or {@link java.util.concurrent.Callable}.
     */
oldratlee's avatar
oldratlee 已提交
42 43
    @Nullable
    public static ExecutorService getTtlExecutorService(@Nullable ExecutorService executorService) {
44 45 46 47 48 49 50 51
        if (executorService == null || executorService instanceof ExecutorServiceTtlWrapper) {
            return executorService;
        }
        return new ExecutorServiceTtlWrapper(executorService);
    }

    /**
     * {@link TransmittableThreadLocal} Wrapper of {@link ScheduledExecutorService},
oldratlee's avatar
oldratlee 已提交
52
     * transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable} or {@link java.util.concurrent.Callable}
53 54
     * to the execution time of {@link Runnable} or {@link java.util.concurrent.Callable}.
     */
oldratlee's avatar
oldratlee 已提交
55 56
    @Nullable
    public static ScheduledExecutorService getTtlScheduledExecutorService(@Nullable ScheduledExecutorService scheduledExecutorService) {
57 58 59 60 61 62
        if (scheduledExecutorService == null || scheduledExecutorService instanceof ScheduledExecutorServiceTtlWrapper) {
            return scheduledExecutorService;
        }
        return new ScheduledExecutorServiceTtlWrapper(scheduledExecutorService);
    }

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    /**
     * check the executor is TTL wrapper executor or not.
     * <p>
     * if the parameter executor is TTL wrapper, return {@code true}, otherwise {@code false}.
     * <p>
     * NOTE: if input executor is {@code null}, return {@code false}.
     *
     * @param executor input executor
     * @param <T>      Executor type
     * @see #getTtlExecutor(Executor)
     * @see #getTtlExecutorService(ExecutorService)
     * @see #getTtlScheduledExecutorService(ScheduledExecutorService)
     * @see #unwrap(Executor)
     * @since 2.8.0
     */
oldratlee's avatar
oldratlee 已提交
78
    public static <T extends Executor> boolean isTtlWrapper(@Nullable T executor) {
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        return (executor instanceof ExecutorTtlWrapper);
    }

    /**
     * Unwrap TTL wrapper executor to the original/underneath one.
     * <p>
     * if the parameter executor is TTL wrapper, return the original/underneath executor;
     * otherwise, just return the input parameter executor.
     * <p>
     * NOTE: if input executor is {@code null}, return {@code null}.
     *
     * @param executor input executor
     * @param <T>      Executor type
     * @see #getTtlExecutor(Executor)
     * @see #getTtlExecutorService(ExecutorService)
     * @see #getTtlScheduledExecutorService(ScheduledExecutorService)
     * @see #isTtlWrapper(Executor)
     * @since 2.8.0
     */
oldratlee's avatar
oldratlee 已提交
98
    @Nullable
99
    @SuppressWarnings("unchecked")
oldratlee's avatar
oldratlee 已提交
100
    public static <T extends Executor> T unwrap(@Nullable T executor) {
101 102 103 104 105
        if (!isTtlWrapper(executor)) return executor;

        return (T) ((ExecutorTtlWrapper) executor).unwrap();
    }

106 107 108
    private TtlExecutors() {
    }
}