Package io.pravega.common.concurrent
Class ExecutorServiceHelpers
- java.lang.Object
-
- io.pravega.common.concurrent.ExecutorServiceHelpers
-
public final class ExecutorServiceHelpers extends java.lang.ObjectHelper methods for ExecutorService.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classExecutorServiceHelpers.Snapshot
-
Constructor Summary
Constructors Constructor Description ExecutorServiceHelpers()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidexecute(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.ExecutorServicegetShrinkingExecutor(int maxThreadCount, int threadTimeout, java.lang.String poolName)Operates like Executors.cachedThreadPool but with a custom thread timeout and pool name.static ExecutorServiceHelpers.SnapshotgetSnapshot(java.util.concurrent.ExecutorService service)Gets a snapshot of the given ExecutorService.static java.util.concurrent.ThreadFactorygetThreadFactory(java.lang.String groupName)Creates and returns a thread factory that will create threads with the given name prefix.static java.util.concurrent.ThreadFactorygetThreadFactory(java.lang.String groupName, int priority)Creates and returns a thread factory that will create threads with the given name prefix and thread priority.static java.util.concurrent.ScheduledExecutorServicenewScheduledThreadPool(int size, java.lang.String poolName)Creates a new ScheduledExecutorService that will use daemon threads with appropriate names the threads.static java.util.concurrent.ScheduledExecutorServicenewScheduledThreadPool(int size, java.lang.String poolName, int threadPriority)Creates a new ScheduledExecutorService that will use daemon threads with specified priority and names.static voidshutdown(java.time.Duration timeout, java.util.concurrent.ExecutorService... pools)Shuts down the given ExecutorServices in two phases: 1.static voidshutdown(java.util.concurrent.ExecutorService... pools)Shuts down the given ExecutorServices in two phases, using a timeout of 5 seconds: 1.
-
-
-
Method Detail
-
getThreadFactory
public 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.- Parameters:
groupName- the name of the threads- Returns:
- a thread factory
-
getThreadFactory
public static java.util.concurrent.ThreadFactory getThreadFactory(java.lang.String groupName, int priority)Creates and returns a thread factory that will create threads with the given name prefix and thread priority.- Parameters:
groupName- the name of the threadspriority- the priority to be assigned to the thread.- Returns:
- a thread factory
-
newScheduledThreadPool
public 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.- Parameters:
size- The number of threads in the threadpoolpoolName- The name of the pool (this will be printed in logs)- Returns:
- A new executor service.
-
newScheduledThreadPool
public static java.util.concurrent.ScheduledExecutorService newScheduledThreadPool(int size, java.lang.String poolName, int threadPriority)Creates a new ScheduledExecutorService that will use daemon threads with specified priority and names.- Parameters:
size- The number of threads in the threadpoolpoolName- The name of the pool (this will be printed in logs)threadPriority- The priority to be assigned to the threads- Returns:
- A new executor service.
-
getSnapshot
public static ExecutorServiceHelpers.Snapshot getSnapshot(java.util.concurrent.ExecutorService service)
Gets a snapshot of the given ExecutorService.- Parameters:
service- The ExecutorService to request a snapshot on.- Returns:
- A Snapshot of the given ExecutorService, or null if not supported.
-
getShrinkingExecutor
public static java.util.concurrent.ExecutorService getShrinkingExecutor(int maxThreadCount, int threadTimeout, java.lang.String poolName)Operates like Executors.cachedThreadPool but with a custom thread timeout and pool name.- Parameters:
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.- Returns:
- A new threadPool
-
execute
public 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.- Parameters:
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.
-
shutdown
public static void shutdown(java.util.concurrent.ExecutorService... pools)
Shuts down the given ExecutorServices in two phases, using a timeout of 5 seconds: 1. Prevents new tasks from being submitted. 2. Awaits for currently running tasks to terminate. If they don't terminate within the given timeout, they will be forcibly cancelled.- Parameters:
pools- The ExecutorServices to shut down.
-
shutdown
public static void shutdown(java.time.Duration timeout, java.util.concurrent.ExecutorService... pools)Shuts down the given ExecutorServices in two phases: 1. Prevents new tasks from being submitted. 2. Awaits for currently running tasks to terminate. If they don't terminate within the given timeout, they will be forcibly cancelled. This is implemented as per the guidelines in the ExecutorService Javadoc: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html- Parameters:
timeout- Grace period that will be given to tasks to complete.pools- The ExecutorServices to shut down.
-
-