Class MoreExecutors
- java.lang.Object
-
- com.google.common.util.concurrent.MoreExecutors
-
@GwtCompatible(emulated=true) public final class MoreExecutors extends Object
- Since:
- 3.0
- Author:
- Eric Fellheimer, Kyle Littlefield, Justin Mahoney
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addDelayedShutdownHook(ExecutorService service, long terminationTimeout, TimeUnit timeUnit)
Add a shutdown hook to wait for thread completion in the givenservice
.static void
addDelayedShutdownHook(ExecutorService service, Duration terminationTimeout)
Add a shutdown hook to wait for thread completion in the givenservice
.static Executor
directExecutor()
static ExecutorService
getExitingExecutorService(ThreadPoolExecutor executor)
Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application is complete.static ExecutorService
getExitingExecutorService(ThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit)
Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application is complete.static ExecutorService
getExitingExecutorService(ThreadPoolExecutor executor, Duration terminationTimeout)
Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application is complete.static ScheduledExecutorService
getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor)
Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when the application is complete.static ScheduledExecutorService
getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit)
Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when the application is complete.static ScheduledExecutorService
getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor, Duration terminationTimeout)
Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when the application is complete.static ListeningExecutorService
listeningDecorator(ExecutorService delegate)
Creates anExecutorService
whosesubmit
andinvokeAll
methods submitListenableFutureTask
instances to the given delegate executor.static ListeningScheduledExecutorService
listeningDecorator(ScheduledExecutorService delegate)
Creates aScheduledExecutorService
whosesubmit
andinvokeAll
methods submitListenableFutureTask
instances to the given delegate executor.static ListeningExecutorService
newDirectExecutorService()
Creates an executor service that runs each task in the thread that invokesexecute/submit
, as inThreadPoolExecutor.CallerRunsPolicy
.static Executor
newSequentialExecutor(Executor delegate)
Returns anExecutor
that runs each task executed sequentially, such that no two tasks are running concurrently.static ThreadFactory
platformThreadFactory()
Returns a default thread factory used to create new threads.static boolean
shutdownAndAwaitTermination(ExecutorService service, long timeout, TimeUnit unit)
Shuts down the given executor service gradually, first disabling new submissions and later, if necessary, cancelling remaining tasks.static boolean
shutdownAndAwaitTermination(ExecutorService service, Duration timeout)
Shuts down the given executor service gradually, first disabling new submissions and later, if necessary, cancelling remaining tasks.
-
-
-
Method Detail
-
getExitingExecutorService
@GwtIncompatible public static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor, Duration terminationTimeout)
Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application is complete. It does so by using daemon threads and adding a shutdown hook to wait for their completion.This is mainly for fixed thread pools. See
Executors.newFixedThreadPool(int)
.- Parameters:
executor
- the executor to modify to make sure it exits when the application is finishedterminationTimeout
- how long to wait for the executor to finish before terminating the JVM- Returns:
- an unmodifiable version of the input which will not hang the JVM
- Since:
- 28.0 (but only since 33.4.0 in the Android flavor)
-
getExitingExecutorService
@GwtIncompatible public static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit)
Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application is complete. It does so by using daemon threads and adding a shutdown hook to wait for their completion.This is mainly for fixed thread pools. See
Executors.newFixedThreadPool(int)
.- Parameters:
executor
- the executor to modify to make sure it exits when the application is finishedterminationTimeout
- how long to wait for the executor to finish before terminating the JVMtimeUnit
- unit of time for the time parameter- Returns:
- an unmodifiable version of the input which will not hang the JVM
-
getExitingExecutorService
@GwtIncompatible public static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor)
Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application is complete. It does so by using daemon threads and adding a shutdown hook to wait for their completion.This method waits 120 seconds before continuing with JVM termination, even if the executor has not finished its work.
This is mainly for fixed thread pools. See
Executors.newFixedThreadPool(int)
.- Parameters:
executor
- the executor to modify to make sure it exits when the application is finished- Returns:
- an unmodifiable version of the input which will not hang the JVM
-
getExitingScheduledExecutorService
@GwtIncompatible public static ScheduledExecutorService getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor, Duration terminationTimeout)
Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when the application is complete. It does so by using daemon threads and adding a shutdown hook to wait for their completion.This is mainly for fixed thread pools. See
Executors.newScheduledThreadPool(int)
.- Parameters:
executor
- the executor to modify to make sure it exits when the application is finishedterminationTimeout
- how long to wait for the executor to finish before terminating the JVM- Returns:
- an unmodifiable version of the input which will not hang the JVM
- Since:
- 28.0 (but only since 33.4.0 in the Android flavor)
-
getExitingScheduledExecutorService
@GwtIncompatible public static ScheduledExecutorService getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit)
Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when the application is complete. It does so by using daemon threads and adding a shutdown hook to wait for their completion.This is mainly for fixed thread pools. See
Executors.newScheduledThreadPool(int)
.- Parameters:
executor
- the executor to modify to make sure it exits when the application is finishedterminationTimeout
- how long to wait for the executor to finish before terminating the JVMtimeUnit
- unit of time for the time parameter- Returns:
- an unmodifiable version of the input which will not hang the JVM
-
getExitingScheduledExecutorService
@GwtIncompatible public static ScheduledExecutorService getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor)
Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when the application is complete. It does so by using daemon threads and adding a shutdown hook to wait for their completion.This method waits 120 seconds before continuing with JVM termination, even if the executor has not finished its work.
This is mainly for fixed thread pools. See
Executors.newScheduledThreadPool(int)
.- Parameters:
executor
- the executor to modify to make sure it exits when the application is finished- Returns:
- an unmodifiable version of the input which will not hang the JVM
-
addDelayedShutdownHook
@GwtIncompatible public static void addDelayedShutdownHook(ExecutorService service, Duration terminationTimeout)
Add a shutdown hook to wait for thread completion in the givenservice
. This is useful if the given service uses daemon threads, and we want to keep the JVM from exiting immediately on shutdown, instead giving these daemon threads a chance to terminate normally.- Parameters:
service
- ExecutorService which uses daemon threadsterminationTimeout
- how long to wait for the executor to finish before terminating the JVM- Since:
- 28.0 (but only since 33.4.0 in the Android flavor)
-
addDelayedShutdownHook
@GwtIncompatible public static void addDelayedShutdownHook(ExecutorService service, long terminationTimeout, TimeUnit timeUnit)
Add a shutdown hook to wait for thread completion in the givenservice
. This is useful if the given service uses daemon threads, and we want to keep the JVM from exiting immediately on shutdown, instead giving these daemon threads a chance to terminate normally.- Parameters:
service
- ExecutorService which uses daemon threadsterminationTimeout
- how long to wait for the executor to finish before terminating the JVMtimeUnit
- unit of time for the time parameter
-
newDirectExecutorService
@GwtIncompatible public static ListeningExecutorService newDirectExecutorService()
Creates an executor service that runs each task in the thread that invokesexecute/submit
, as inThreadPoolExecutor.CallerRunsPolicy
. This applies both to individually submitted tasks and to collections of tasks submitted viainvokeAll
orinvokeAny
. In the latter case, tasks will run serially on the calling thread. Tasks are run to completion before aFuture
is returned to the caller (unless the executor has been shutdown).Although all tasks are immediately executed in the thread that submitted the task, this
ExecutorService
imposes a small locking overhead on each task submission in order to implement shutdown and termination behavior.The implementation deviates from the
ExecutorService
specification with regards to theshutdownNow
method. First, "best-effort" with regards to canceling running tasks is implemented as "no-effort". No interrupts or other attempts are made to stop threads executing tasks. Second, the returned list will always be empty, as any submitted task is considered to have started execution. This applies also to tasks given toinvokeAll
orinvokeAny
which are pending serial execution, even the subset of the tasks that have not yet started execution. It is unclear from theExecutorService
specification if these should be included, and it's much easier to implement the interpretation that they not be. Finally, a call toshutdown
orshutdownNow
may result in concurrent calls toinvokeAll/invokeAny
throwing RejectedExecutionException, although a subset of the tasks may already have been executed.- Since:
- 18.0 (present as MoreExecutors.sameThreadExecutor() since 10.0)
-
directExecutor
public static Executor directExecutor()
Returns anExecutor
that runs each task in the thread that invokesexecute
, as inThreadPoolExecutor.CallerRunsPolicy
.This executor is appropriate for tasks that are lightweight and not deeply chained. Inappropriate
directExecutor
usage can cause problems, and these problems can be difficult to reproduce because they depend on timing. For example:- When a
ListenableFuture
listener is registered to run underdirectExecutor
, the listener can execute in any of three possible threads:- When a thread attaches a listener to a
ListenableFuture
that's already complete, the listener runs immediately in that thread. - When a thread attaches a listener to a
ListenableFuture
that's incomplete and theListenableFuture
later completes normally, the listener runs in the thread that completes theListenableFuture
. - When a listener is attached to a
ListenableFuture
and theListenableFuture
gets cancelled, the listener runs immediately in the thread that cancelled theFuture
.
- When a thread attaches a listener to a
- If many tasks will be triggered by the same event, one heavyweight task may delay other
tasks -- even tasks that are not themselves
directExecutor
tasks. - If many such tasks are chained together (such as with
future.transform(...).transform(...).transform(...)....
), they may overflow the stack. (In simple cases, callers can avoid this by registering all tasks with the samenewSequentialExecutor(java.util.concurrent.Executor)
wrapper arounddirectExecutor()
. More complex cases may require using thread pools or making deeper changes.) - If an exception propagates out of a
Runnable
, it is not necessarily seen by anyUncaughtExceptionHandler
for the thread. For example, if the callback passed toFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)
throws an exception, that exception will be typically be logged by theListenableFuture
implementation, even if the thread is configured to do something different. In other cases, no code will catch the exception, and it may terminate whichever thread happens to trigger the execution.
ListenableFuture
listeners, should take care not to do so while holding a lock. Additionally, as a further line of defense, prefer not to perform any locking inside a task that will be run underdirectExecutor
: Not only might the wait for a lock be long, but if the running thread was holding a lock, the listener may deadlock or break lock isolation.This instance is equivalent to:
final class DirectExecutor implements Executor { public void execute(Runnable r) { r.run(); } }
This should be preferred to
newDirectExecutorService()
because implementing theExecutorService
subinterface necessitates significant performance overhead.- Since:
- 18.0
- When a
-
newSequentialExecutor
@GwtIncompatible public static Executor newSequentialExecutor(Executor delegate)
Returns anExecutor
that runs each task executed sequentially, such that no two tasks are running concurrently.executed tasks have a happens-before order as defined in the Java Language Specification. Tasks execute with the same happens-before order that the function calls to
execute()
that submitted those tasks had.The executor uses
delegate
in order toexecute
each task in turn, and does not create any threads of its own.After execution begins on a thread from the
delegate
Executor
, tasks are polled and executed from a task queue until there are no more tasks. The thread will not be released until there are no more tasks to run.If a task is submitted while a thread is executing tasks from the task queue, the thread will not be released until that submitted task is also complete.
If a task is interrupted while a task is running:
- execution will not stop until the task queue is empty.
- tasks will begin execution with the thread marked as not interrupted - any interruption applies only to the task that was running at the point of interruption.
- if the thread was interrupted before the SequentialExecutor's worker begins execution,
the interrupt will be restored to the thread after it completes so that its
delegate
Executor may process the interrupt. - subtasks are run with the thread uninterrupted and interrupts received during execution of a task are ignored.
RuntimeException
s thrown by tasks are simply logged and the executor keeps trucking. If anError
is thrown, the error will propagate and execution will stop until the next time a task is submitted.When an
Error
is thrown by an executed task, previously submitted tasks may never run. An attempt will be made to restart execution on the next call toexecute
. If thedelegate
has begun to reject execution, the previously submitted tasks may never run, despite not throwing a RejectedExecutionException synchronously with the call toexecute
. If this behaviour is problematic, use an Executor with a single thread (e.g.Executors.newSingleThreadExecutor()
).- Since:
- 23.3 (since 23.1 as
sequentialExecutor
)
-
listeningDecorator
@GwtIncompatible public static ListeningExecutorService listeningDecorator(ExecutorService delegate)
Creates anExecutorService
whosesubmit
andinvokeAll
methods submitListenableFutureTask
instances to the given delegate executor. Those methods, as well asexecute
andinvokeAny
, are implemented in terms of calls todelegate.execute
. All other methods are forwarded unchanged to the delegate. This implies that the returnedListeningExecutorService
never calls the delegate'ssubmit
,invokeAll
, andinvokeAny
methods, so any special handling of tasks must be implemented in the delegate'sexecute
method or by wrapping the returnedListeningExecutorService
.If the delegate executor was already an instance of
ListeningExecutorService
, it is returned untouched, and the rest of this documentation does not apply.- Since:
- 10.0
-
listeningDecorator
@GwtIncompatible public static ListeningScheduledExecutorService listeningDecorator(ScheduledExecutorService delegate)
Creates aScheduledExecutorService
whosesubmit
andinvokeAll
methods submitListenableFutureTask
instances to the given delegate executor. Those methods, as well asexecute
andinvokeAny
, are implemented in terms of calls todelegate.execute
. All other methods are forwarded unchanged to the delegate. This implies that the returnedListeningScheduledExecutorService
never calls the delegate'ssubmit
,invokeAll
, andinvokeAny
methods, so any special handling of tasks must be implemented in the delegate'sexecute
method or by wrapping the returnedListeningScheduledExecutorService
.If the delegate executor was already an instance of
ListeningScheduledExecutorService
, it is returned untouched, and the rest of this documentation does not apply.- Since:
- 10.0
-
platformThreadFactory
@GwtIncompatible public static ThreadFactory platformThreadFactory()
Returns a default thread factory used to create new threads.When running on AppEngine with access to AppEngine legacy APIs, this method returns
ThreadManager.currentRequestThreadFactory()
. Otherwise, it returnsExecutors.defaultThreadFactory()
.- Since:
- 14.0
-
shutdownAndAwaitTermination
@CanIgnoreReturnValue @GwtIncompatible public static boolean shutdownAndAwaitTermination(ExecutorService service, Duration timeout)
Shuts down the given executor service gradually, first disabling new submissions and later, if necessary, cancelling remaining tasks.The method takes the following steps:
- calls
ExecutorService.shutdown()
, disabling acceptance of new submitted tasks. - awaits executor service termination for half of the specified timeout.
- if the timeout expires, it calls
ExecutorService.shutdownNow()
, cancelling pending tasks and interrupting running tasks. - awaits executor service termination for the other half of the specified timeout.
If, at any step of the process, the calling thread is interrupted, the method calls
ExecutorService.shutdownNow()
and returns.- Parameters:
service
- theExecutorService
to shut downtimeout
- the maximum time to wait for theExecutorService
to terminate- Returns:
true
if theExecutorService
was terminated successfully,false
if the call timed out or was interrupted- Since:
- 28.0 (but only since 33.4.0 in the Android flavor)
- calls
-
shutdownAndAwaitTermination
@CanIgnoreReturnValue @GwtIncompatible public static boolean shutdownAndAwaitTermination(ExecutorService service, long timeout, TimeUnit unit)
Shuts down the given executor service gradually, first disabling new submissions and later, if necessary, cancelling remaining tasks.The method takes the following steps:
- calls
ExecutorService.shutdown()
, disabling acceptance of new submitted tasks. - awaits executor service termination for half of the specified timeout.
- if the timeout expires, it calls
ExecutorService.shutdownNow()
, cancelling pending tasks and interrupting running tasks. - awaits executor service termination for the other half of the specified timeout.
If, at any step of the process, the calling thread is interrupted, the method calls
ExecutorService.shutdownNow()
and returns.- Parameters:
service
- theExecutorService
to shut downtimeout
- the maximum time to wait for theExecutorService
to terminateunit
- the time unit of the timeout argument- Returns:
true
if theExecutorService
was terminated successfully,false
if the call timed out or was interrupted- Since:
- 17.0
- calls
-
-