Class MoreExecutors
- java.lang.Object
-
- com.google.common.util.concurrent.MoreExecutors
-
public final class MoreExecutors extends java.lang.Object
Factory and utility methods forExecutor
,ExecutorService
, andThreadFactory
.- Since:
- 3.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addDelayedShutdownHook(java.util.concurrent.ExecutorService service, long terminationTimeout, java.util.concurrent.TimeUnit timeUnit)
Add a shutdown hook to wait for thread completion in the givenservice
.static java.util.concurrent.ExecutorService
getExitingExecutorService(java.util.concurrent.ThreadPoolExecutor executor)
Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application is complete.static java.util.concurrent.ExecutorService
getExitingExecutorService(java.util.concurrent.ThreadPoolExecutor executor, long terminationTimeout, java.util.concurrent.TimeUnit timeUnit)
Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application is complete.static java.util.concurrent.ScheduledExecutorService
getExitingScheduledExecutorService(java.util.concurrent.ScheduledThreadPoolExecutor executor)
Converts the given ThreadPoolExecutor into a ScheduledExecutorService that exits when the application is complete.static java.util.concurrent.ScheduledExecutorService
getExitingScheduledExecutorService(java.util.concurrent.ScheduledThreadPoolExecutor executor, long terminationTimeout, java.util.concurrent.TimeUnit timeUnit)
Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when the application is complete.static ListeningExecutorService
listeningDecorator(java.util.concurrent.ExecutorService delegate)
Creates anExecutorService
whosesubmit
andinvokeAll
methods submitListenableFutureTask
instances to the given delegate executor.static ListeningScheduledExecutorService
listeningDecorator(java.util.concurrent.ScheduledExecutorService delegate)
Creates aScheduledExecutorService
whosesubmit
andinvokeAll
methods submitListenableFutureTask
instances to the given delegate executor.static java.util.concurrent.ThreadFactory
platformThreadFactory()
Returns a default thread factory used to create new threads.static ListeningExecutorService
sameThreadExecutor()
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
.
-
-
-
Method Detail
-
getExitingExecutorService
@Beta public static java.util.concurrent.ExecutorService getExitingExecutorService(java.util.concurrent.ThreadPoolExecutor executor, long terminationTimeout, java.util.concurrent.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
-
getExitingScheduledExecutorService
@Beta public static java.util.concurrent.ScheduledExecutorService getExitingScheduledExecutorService(java.util.concurrent.ScheduledThreadPoolExecutor executor, long terminationTimeout, java.util.concurrent.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
-
addDelayedShutdownHook
@Beta public static void addDelayedShutdownHook(java.util.concurrent.ExecutorService service, long terminationTimeout, java.util.concurrent.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
-
getExitingExecutorService
@Beta public static java.util.concurrent.ExecutorService getExitingExecutorService(java.util.concurrent.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
@Beta public static java.util.concurrent.ScheduledExecutorService getExitingScheduledExecutorService(java.util.concurrent.ScheduledThreadPoolExecutor executor)
Converts the given ThreadPoolExecutor 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
-
sameThreadExecutor
public static ListeningExecutorService sameThreadExecutor()
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:
- 10.0 (mostly source-compatible since 3.0)
-
listeningDecorator
public static ListeningExecutorService listeningDecorator(java.util.concurrent.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
public static ListeningScheduledExecutorService listeningDecorator(java.util.concurrent.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
@Beta public static java.util.concurrent.ThreadFactory platformThreadFactory()
Returns a default thread factory used to create new threads.On AppEngine, returns
ThreadManager.currentRequestThreadFactory()
. Otherwise, returnsExecutors.defaultThreadFactory()
.- Since:
- 14.0
-
-