Class TtlExecutors
- java.lang.Object
-
- com.alibaba.ttl.threadpool.TtlExecutors
-
public final class TtlExecutors extends Object
Util methods for TTL wrapper of jdk executors.- wrap/check/unwrap methods for TTL wrapper of jdk executors(
Executor,ExecutorService,ScheduledExecutorService). - wrap/check/unwrap methods for disable Inheritable wrapper of
ThreadFactory. - wrap/check/unwrap methods for
TtlRunnableUnwrapComparatorwrapper ofPriorityBlockingQueue.
Note:
- all method is
null-safe. for wrap/unwrap methods when input parameter isnull, returnnull. for check methods when input parameter isnull, returnfalse. - skip wrap/decoration thread pool/
executor(aka. just return inputexecutor) when ttl agent is loaded, Or when inputexecutoris already wrapped/decorated.
- Since:
- 0.9.0
- Author:
- Jerry Lee (oldratlee at gmail dot com)
- See Also:
Executor,ExecutorService,ScheduledExecutorService,ThreadPoolExecutor,ScheduledThreadPoolExecutor,Executors,CompletionService,ExecutorCompletionService,ThreadFactory,Executors.defaultThreadFactory(),PriorityBlockingQueue,TtlForkJoinPoolHelper
- wrap/check/unwrap methods for TTL wrapper of jdk executors(
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ThreadFactorygetDefaultDisableInheritableThreadFactory()Wrapper ofExecutors.defaultThreadFactory(), disable inheritable.static ThreadFactorygetDisableInheritableThreadFactory(ThreadFactory threadFactory)Wrapper ofThreadFactory, disable inheritable.static ExecutorgetTtlExecutor(Executor executor)TransmittableThreadLocalWrapper ofExecutor, transmit theTransmittableThreadLocalfrom the task submit time ofRunnableto the execution time ofRunnable.static ExecutorServicegetTtlExecutorService(ExecutorService executorService)TransmittableThreadLocalWrapper ofExecutorService, transmit theTransmittableThreadLocalfrom the task submit time ofRunnableorCallableto the execution time ofRunnableorCallable.static Comparator<Runnable>getTtlRunnableUnwrapComparator(Comparator<Runnable> comparator)Wrapper ofComparator<Runnable>which unwrapTtlRunnablebefore compare, akaTtlRunnableUnwrapComparator.static Comparator<Runnable>getTtlRunnableUnwrapComparatorForComparableRunnable()TtlRunnableUnwrapComparatorthat comparesComparableobjects.static ScheduledExecutorServicegetTtlScheduledExecutorService(ScheduledExecutorService scheduledExecutorService)TransmittableThreadLocalWrapper ofScheduledExecutorService, transmit theTransmittableThreadLocalfrom the task submit time ofRunnableorCallableto the execution time ofRunnableorCallable.static booleanisDisableInheritableThreadFactory(ThreadFactory threadFactory)check theThreadFactoryisDisableInheritableThreadFactoryor not.static booleanisTtlRunnableUnwrapComparator(Comparator<Runnable> comparator)check theComparator<Runnable>is a wrapperTtlRunnableUnwrapComparatoror not.static <T extends Executor>
booleanisTtlWrapper(T executor)check the executor is a TTL executor wrapper or not.static Comparator<Runnable>unwrap(Comparator<Runnable> comparator)UnwrapTtlRunnableUnwrapComparatorto the original/underneathComparator<Runnable>.static ThreadFactoryunwrap(ThreadFactory threadFactory)UnwrapDisableInheritableThreadFactoryto the original/underneath one.static <T extends Executor>
Tunwrap(T executor)Unwrap TTL executor wrapper to the original/underneath one.
-
-
-
Method Detail
-
getTtlExecutor
@Nullable @Contract(value="null -> null; !null -> !null", pure=true) public static Executor getTtlExecutor(@Nullable Executor executor)
TransmittableThreadLocalWrapper ofExecutor, transmit theTransmittableThreadLocalfrom the task submit time ofRunnableto the execution time ofRunnable.NOTE: sine v2.12.0 the idempotency of return wrapped Executor is changed to true, so the wrapped Executor can be cooperated with the usage of "Decorate Runnable and Callable".
About idempotency: if is idempotent, it's allowed to submit the
TtlRunnable/TtlCallableto the wrapped Executor; otherwise throwIllegalStateException.- Parameters:
executor- input Executor- Returns:
- wrapped Executor
- See Also:
TtlRunnable.get(Runnable, boolean, boolean),TtlCallable.get(Callable, boolean, boolean)
-
getTtlExecutorService
@Nullable @Contract(value="null -> null; !null -> !null", pure=true) public static ExecutorService getTtlExecutorService(@Nullable ExecutorService executorService)
TransmittableThreadLocalWrapper ofExecutorService, transmit theTransmittableThreadLocalfrom the task submit time ofRunnableorCallableto the execution time ofRunnableorCallable.NOTE: sine v2.12.0 the idempotency of return wrapped ExecutorService is changed to true, so the wrapped ExecutorService can be cooperated with the usage of "Decorate Runnable and Callable".
About idempotency: if is idempotent, it's allowed to submit the
TtlRunnable/TtlCallableto the wrapped ExecutorService; otherwise throwIllegalStateException.- Parameters:
executorService- input ExecutorService- Returns:
- wrapped ExecutorService
- See Also:
TtlRunnable.get(Runnable, boolean, boolean),TtlCallable.get(Callable, boolean, boolean)
-
getTtlScheduledExecutorService
@Nullable @Contract(value="null -> null; !null -> !null", pure=true) public static ScheduledExecutorService getTtlScheduledExecutorService(@Nullable ScheduledExecutorService scheduledExecutorService)
TransmittableThreadLocalWrapper ofScheduledExecutorService, transmit theTransmittableThreadLocalfrom the task submit time ofRunnableorCallableto the execution time ofRunnableorCallable.NOTE: sine v2.12.0 the idempotency of return wrapped ScheduledExecutorService is changed to true, so the wrapped ScheduledExecutorService can be cooperated with the usage of "Decorate Runnable and Callable".
About idempotency: if is idempotent, it's allowed to submit the
TtlRunnable/TtlCallableto the wrapped ScheduledExecutorService; otherwise throwIllegalStateException.- Parameters:
scheduledExecutorService- input scheduledExecutorService- Returns:
- wrapped scheduledExecutorService
- See Also:
TtlRunnable.get(Runnable, boolean, boolean),TtlCallable.get(Callable, boolean, boolean)
-
isTtlWrapper
public static <T extends Executor> boolean isTtlWrapper(@Nullable T executor)
check the executor is a TTL executor wrapper or not.if the parameter executor is TTL wrapper, return
true, otherwisefalse.NOTE: if input executor is
null, returnfalse.- Type Parameters:
T- Executor type- Parameters:
executor- input executor- Since:
- 2.8.0
- See Also:
getTtlExecutor(Executor),getTtlExecutorService(ExecutorService),getTtlScheduledExecutorService(ScheduledExecutorService),unwrap(Executor)
-
unwrap
@Nullable @Contract(value="null -> null; !null -> !null", pure=true) public static <T extends Executor> T unwrap(@Nullable T executor)
Unwrap TTL executor wrapper to the original/underneath one.if the parameter executor is TTL wrapper, return the original/underneath executor; otherwise, just return the input parameter executor.
NOTE: if input executor is
null, returnnull.- Type Parameters:
T- Executor type- Parameters:
executor- input executor- Since:
- 2.8.0
- See Also:
getTtlExecutor(Executor),getTtlExecutorService(ExecutorService),getTtlScheduledExecutorService(ScheduledExecutorService),isTtlWrapper(Executor),TtlUnwrap.unwrap(Object)
-
getDisableInheritableThreadFactory
@Nullable @Contract(value="null -> null; !null -> !null", pure=true) public static ThreadFactory getDisableInheritableThreadFactory(@Nullable ThreadFactory threadFactory)
Wrapper ofThreadFactory, disable inheritable.- Parameters:
threadFactory- input thread factory- Since:
- 2.10.0
- See Also:
DisableInheritableThreadFactory,TtlForkJoinPoolHelper.getDisableInheritableForkJoinWorkerThreadFactory(java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory)
-
getDefaultDisableInheritableThreadFactory
@NonNull public static ThreadFactory getDefaultDisableInheritableThreadFactory()
Wrapper ofExecutors.defaultThreadFactory(), disable inheritable.
-
isDisableInheritableThreadFactory
public static boolean isDisableInheritableThreadFactory(@Nullable ThreadFactory threadFactory)
check theThreadFactoryisDisableInheritableThreadFactoryor not.
-
unwrap
@Nullable @Contract(value="null -> null; !null -> !null", pure=true) public static ThreadFactory unwrap(@Nullable ThreadFactory threadFactory)
UnwrapDisableInheritableThreadFactoryto the original/underneath one.
-
getTtlRunnableUnwrapComparator
@Nullable @Contract(value="null -> null; !null -> !null", pure=true) public static Comparator<Runnable> getTtlRunnableUnwrapComparator(@Nullable Comparator<Runnable> comparator)
Wrapper ofComparator<Runnable>which unwrapTtlRunnablebefore compare, akaTtlRunnableUnwrapComparator.Prepared for
comparatorparameter of constructorPriorityBlockingQueue(int, Comparator).PriorityBlockingQueuecan be used by constructorThreadPoolExecutor(int, int, long, java.util.concurrent.TimeUnit, java.util.concurrent.BlockingQueue).- Parameters:
comparator- input comparator- Returns:
- wrapped comparator
- Since:
- 2.12.3
- See Also:
ThreadPoolExecutor,ThreadPoolExecutor(int, int, long, java.util.concurrent.TimeUnit, java.util.concurrent.BlockingQueue),PriorityBlockingQueue,PriorityBlockingQueue(int, Comparator)
-
getTtlRunnableUnwrapComparatorForComparableRunnable
@NonNull public static Comparator<Runnable> getTtlRunnableUnwrapComparatorForComparableRunnable()
TtlRunnableUnwrapComparatorthat comparesComparableobjects.- Since:
- 2.12.3
- See Also:
getTtlRunnableUnwrapComparator(Comparator)
-
isTtlRunnableUnwrapComparator
public static boolean isTtlRunnableUnwrapComparator(@Nullable Comparator<Runnable> comparator)
check theComparator<Runnable>is a wrapperTtlRunnableUnwrapComparatoror not.- Since:
- 2.12.3
- See Also:
getTtlRunnableUnwrapComparator(Comparator),getTtlRunnableUnwrapComparatorForComparableRunnable()
-
unwrap
@Nullable @Contract(value="null -> null; !null -> !null", pure=true) public static Comparator<Runnable> unwrap(@Nullable Comparator<Runnable> comparator)
UnwrapTtlRunnableUnwrapComparatorto the original/underneathComparator<Runnable>.
-
-