| Modifier and Type | Field and Description |
|---|---|
static long |
EXECUTOR_SERVICE_SHUTDOWN_TIMEOUT |
static TimeUnit |
EXECUTOR_SERVICE_SHUTDOWN_TIMEOUT_TIMEUNIT |
| Constructor and Description |
|---|
ExecutorsUtils() |
| Modifier and Type | Method and Description |
|---|---|
static ThreadFactory |
defaultThreadFactory()
Get a default
ThreadFactory. |
static <T> Callable<T> |
loggingDecorator(Callable<T> callable)
Creates an
Callable which propagates the MDC
information across thread boundaries. |
static com.google.common.util.concurrent.ListeningExecutorService |
loggingDecorator(ExecutorService executorService)
Creates an
ListeningExecutorService whose submit
and execute methods propagate the MDC information across
thread boundaries. |
static Runnable |
loggingDecorator(Runnable runnable)
Creates an
Runnable which propagates the MDC
information across thread boundaries. |
static com.google.common.util.concurrent.ListeningScheduledExecutorService |
loggingDecorator(ScheduledExecutorService scheduledExecutorService)
Creates an
ListeningScheduledExecutorService whose
submit, execute, schedule,
scheduleAtFixedRate, and scheduleWithFixedDelay
methods propagate the MDC information across thread boundaries. |
static ThreadFactory |
newDaemonThreadFactory(com.google.common.base.Optional<org.slf4j.Logger> logger,
com.google.common.base.Optional<String> nameFormat)
Get a new
ThreadFactory that uses a LoggingUncaughtExceptionHandler
to handle uncaught exceptions, uses the given thread name format, and produces daemon threads. |
static ThreadFactory |
newThreadFactory(com.google.common.base.Optional<org.slf4j.Logger> logger)
Get a new
ThreadFactory that uses a LoggingUncaughtExceptionHandler
to handle uncaught exceptions. |
static ThreadFactory |
newThreadFactory(com.google.common.base.Optional<org.slf4j.Logger> logger,
com.google.common.base.Optional<String> nameFormat)
Get a new
ThreadFactory that uses a LoggingUncaughtExceptionHandler
to handle uncaught exceptions and the given thread name format. |
static <F,T> List<T> |
parallelize(List<F> list,
com.google.common.base.Function<F,T> function,
int threadCount,
int timeoutInSecs,
com.google.common.base.Optional<org.slf4j.Logger> logger)
A utility method to parallelize loops.
|
static void |
shutdownExecutorService(ExecutorService executorService,
com.google.common.base.Optional<org.slf4j.Logger> logger)
Shutdown an
ExecutorService gradually, first disabling new task submissions and
later cancelling existing tasks. |
static void |
shutdownExecutorService(ExecutorService executorService,
com.google.common.base.Optional<org.slf4j.Logger> logger,
long timeout,
TimeUnit unit)
Shutdown an
ExecutorService gradually, first disabling new task submissions and later cancelling
existing tasks. |
public static final long EXECUTOR_SERVICE_SHUTDOWN_TIMEOUT
public static final TimeUnit EXECUTOR_SERVICE_SHUTDOWN_TIMEOUT_TIMEUNIT
public static ThreadFactory defaultThreadFactory()
ThreadFactory.ThreadFactorypublic static ThreadFactory newThreadFactory(com.google.common.base.Optional<org.slf4j.Logger> logger)
ThreadFactory that uses a LoggingUncaughtExceptionHandler
to handle uncaught exceptions.logger - an Optional wrapping the Logger that the
LoggingUncaughtExceptionHandler uses to log uncaught exceptions thrown in threadsThreadFactorypublic static ThreadFactory newThreadFactory(com.google.common.base.Optional<org.slf4j.Logger> logger, com.google.common.base.Optional<String> nameFormat)
ThreadFactory that uses a LoggingUncaughtExceptionHandler
to handle uncaught exceptions and the given thread name format.logger - an Optional wrapping the Logger that the
LoggingUncaughtExceptionHandler uses to log uncaught exceptions thrown in threadsnameFormat - an Optional wrapping a thread naming formatThreadFactorypublic static ThreadFactory newDaemonThreadFactory(com.google.common.base.Optional<org.slf4j.Logger> logger, com.google.common.base.Optional<String> nameFormat)
ThreadFactory that uses a LoggingUncaughtExceptionHandler
to handle uncaught exceptions, uses the given thread name format, and produces daemon threads.logger - an Optional wrapping the Logger that the
LoggingUncaughtExceptionHandler uses to log uncaught exceptions thrown in threadsnameFormat - an Optional wrapping a thread naming formatThreadFactorypublic static com.google.common.util.concurrent.ListeningExecutorService loggingDecorator(ExecutorService executorService)
ListeningExecutorService whose submit
and execute methods propagate the MDC information across
thread boundaries.executorService - the ExecutorService to wrapListeningExecutorServicepublic static com.google.common.util.concurrent.ListeningScheduledExecutorService loggingDecorator(ScheduledExecutorService scheduledExecutorService)
ListeningScheduledExecutorService whose
submit, execute, schedule,
scheduleAtFixedRate, and scheduleWithFixedDelay
methods propagate the MDC information across thread boundaries.scheduledExecutorService - the ScheduledExecutorService to wrapListeningScheduledExecutorServicepublic static Runnable loggingDecorator(Runnable runnable)
Runnable which propagates the MDC
information across thread boundaries.public static <T> Callable<T> loggingDecorator(Callable<T> callable)
Callable which propagates the MDC
information across thread boundaries.callable - the Callable to wrapCallablepublic static void shutdownExecutorService(ExecutorService executorService, com.google.common.base.Optional<org.slf4j.Logger> logger, long timeout, TimeUnit unit)
ExecutorService gradually, first disabling new task submissions and later cancelling
existing tasks.
The implementation is based on the implementation of Guava's MoreExecutors.shutdownAndAwaitTermination, which is available since version 17.0. We cannot use Guava version 17.0 or after directly, however, as it cannot be used with Hadoop 2.6.0 or after due to the issue reported in HADOOP-10961.
executorService - the ExecutorService to shutdownlogger - an Optional wrapping the Logger that is used to log metadata of the executorService
if it cannot shutdown all its threadstimeout - the maximum time to wait for the ExecutorService to terminateunit - the time unit of the timeout argumentpublic static void shutdownExecutorService(ExecutorService executorService, com.google.common.base.Optional<org.slf4j.Logger> logger)
ExecutorService gradually, first disabling new task submissions and
later cancelling existing tasks.
This method calls shutdownExecutorService(ExecutorService, Optional, long, TimeUnit)
with default timeout time EXECUTOR_SERVICE_SHUTDOWN_TIMEOUT and time unit
EXECUTOR_SERVICE_SHUTDOWN_TIMEOUT_TIMEUNIT.
executorService - the ExecutorService to shutdownlogger - an Optional wrapping a Logger to be used during shutdownpublic static <F,T> List<T> parallelize(List<F> list, com.google.common.base.Function<F,T> function, int threadCount, int timeoutInSecs, com.google.common.base.Optional<org.slf4j.Logger> logger) throws ExecutionException
Function to every element in the List in
parallel by spawning threads. A list containing the result obtained by applying the function is returned. The
method is a blocking call and will wait for all the elements in the list to be processed or timeoutInSecs which
ever is earlier.
NOTE: The method is an all or none implementation. Meaning, if any of the thread fails, the method will throw an
ExecutionException even if other threads completed successfully
shutdownExecutorService(ExecutorService, Optional, long, TimeUnit) to shutdown the executor
service
list - input list on which the function is applied in parallelfunction - to be applied on every element of the listthreadCount - to be used to process the listtimeoutInSecs - to wait for all the threads to completelogger - an Optional wrapping a Logger to be used during shutdownIllegalArgumentException - if input list or function is nullExecutionException -