Class MoreExecutors
- Since:
- 3.0
- Author:
- Eric Fellheimer, Kyle Littlefield, Justin Mahoney
-
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutorReturns anExecutorthat runs each task in the thread that invokesexecute, as inThreadPoolExecutor.CallerRunsPolicy.static ListeningExecutorServiceCreates an executor service that runs each task in the thread that invokesexecute/submit, as inThreadPoolExecutor.CallerRunsPolicyThis applies both to individually submitted tasks and to collections of tasks submitted viainvokeAllorinvokeAny.static ThreadFactoryReturns a default thread factory used to create new threads.static booleanshutdownAndAwaitTermination(ExecutorService service, long timeout, TimeUnit unit) Shuts down the given executor service gradually, first disabling new submissions and later, if necessary, cancelling remaining tasks.
-
Method Details
-
newDirectExecutorService
Creates an executor service that runs each task in the thread that invokesexecute/submit, as inThreadPoolExecutor.CallerRunsPolicyThis applies both to individually submitted tasks and to collections of tasks submitted viainvokeAllorinvokeAny. In the latter case, tasks will run serially on the calling thread. Tasks are run to completion before aFutureis returned to the caller (unless the executor has been shutdown).Although all tasks are immediately executed in the thread that submitted the task, this
ExecutorServiceimposes a small locking overhead on each task submission in order to implement shutdown and termination behavior.The implementation deviates from the
ExecutorServicespecification with regards to theshutdownNowmethod. 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 toinvokeAllorinvokeAnywhich are pending serial execution, even the subset of the tasks that have not yet started execution. It is unclear from theExecutorServicespecification if these should be included, and it's much easier to implement the interpretation that they not be. Finally, a call toshutdownorshutdownNowmay result in concurrent calls toinvokeAll/invokeAnythrowing RejectedExecutionException, although a subset of the tasks may already have been executed.- Since:
- 18.0 (present as MoreExecutors.sameThreadExecutor() since 10.0)
-
directExecutor
Returns anExecutorthat runs each task in the thread that invokesexecute, as inThreadPoolExecutor.CallerRunsPolicy.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 theExecutorServicesubinterface necessitates significant performance overhead.- Since:
- 18.0
-
platformThreadFactory
Returns a default thread factory used to create new threads.On AppEngine, returns
ThreadManager.currentRequestThreadFactory(). Otherwise, returnsExecutors.defaultThreadFactory().- Since:
- 14.0
-
shutdownAndAwaitTermination
@Beta @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- theExecutorServiceto shut downtimeout- the maximum time to wait for theExecutorServiceto terminateunit- the time unit of the timeout argument- Returns:
trueif theExecutorServicewas terminated successfully,falseif the call timed out or was interrupted- Since:
- 17.0
- calls
-