public final class ExecutorServiceHelpers
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static class |
ExecutorServiceHelpers.Snapshot |
| Constructor and Description |
|---|
ExecutorServiceHelpers() |
| Modifier and Type | Method and Description |
|---|---|
static void |
execute(RunnableWithException task,
java.util.function.Consumer<java.lang.Throwable> exceptionHandler,
java.lang.Runnable runFinally,
java.util.concurrent.Executor executor)
Executes the given task on the given Executor.
|
static java.util.concurrent.ThreadPoolExecutor |
getShrinkingExecutor(int maxThreadCount,
int threadTimeout,
java.lang.String poolName)
Operates like Executors.cachedThreadPool but with a custom thread timeout and pool name.
|
static ExecutorServiceHelpers.Snapshot |
getSnapshot(java.util.concurrent.ExecutorService service)
Gets a snapshot of the given ExecutorService.
|
static java.util.concurrent.ThreadFactory |
getThreadFactory(java.lang.String groupName)
Creates and returns a thread factory that will create threads with the given name prefix.
|
static java.util.concurrent.ScheduledExecutorService |
newScheduledThreadPool(int size,
java.lang.String poolName)
Creates a new ScheduledExecutorService that will use daemon threads with appropriate names the threads.
|
static void |
shutdown(java.time.Duration timeout,
java.util.concurrent.ExecutorService... pools)
Shuts down the given ExecutorServices in two phases:
1.
|
static void |
shutdown(java.util.concurrent.ExecutorService... pools)
Shuts down the given ExecutorServices in two phases, using a timeout of 5 seconds:
1.
|
public static java.util.concurrent.ThreadFactory getThreadFactory(java.lang.String groupName)
groupName - the name of the threadspublic static java.util.concurrent.ScheduledExecutorService newScheduledThreadPool(int size,
java.lang.String poolName)
size - The number of threads in the threadpoolpoolName - The name of the pool (this will be printed in logs)public static ExecutorServiceHelpers.Snapshot getSnapshot(java.util.concurrent.ExecutorService service)
service - The ExecutorService to request a snapshot on.public static java.util.concurrent.ThreadPoolExecutor getShrinkingExecutor(int maxThreadCount,
int threadTimeout,
java.lang.String poolName)
maxThreadCount - The maximum number of threads to allow in the pool.threadTimeout - the number of milliseconds that a thread should sit idle before shutting down.poolName - The name of the threadpool.public static void execute(RunnableWithException task, java.util.function.Consumer<java.lang.Throwable> exceptionHandler, java.lang.Runnable runFinally, java.util.concurrent.Executor executor)
task - The RunnableWithException to execute.exceptionHandler - A Consumer that will be invoked in case the task threw an Exception. This is not invoked if
the executor could not execute the given task.runFinally - A Runnable that is guaranteed to be invoked at the end of this execution. If the executor
did accept the task, it will be invoked after the task is complete (or ended in failure).
If the executor did not accept the task, it will be executed when this method returns.executor - An Executor to execute the task on.public static void shutdown(java.util.concurrent.ExecutorService... pools)
pools - The ExecutorServices to shut down.public static void shutdown(java.time.Duration timeout,
java.util.concurrent.ExecutorService... pools)
timeout - Grace period that will be given to tasks to complete.pools - The ExecutorServices to shut down.