Class MoreExecutors
- Since:
- 3.0
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutorReturns anExecutorthat runs each task in the thread that invokesexecute, as inThreadPoolExecutor.CallerRunsPolicy.private static booleanstatic 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.(package private) static ThreadCreates a thread usingplatformThreadFactory(), and sets its name tonameunless changing the name is forbidden by the security manager.static ThreadFactoryReturns a default thread factory used to create new threads.(package private) static ExecutorrejectionPropagatingExecutor(Executor delegate, AbstractFuture<?> future) Returns an Executor that will propagateRejectedExecutionExceptionfrom the delegate executor to the givenfuture.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.private static <T> ListenableFuture<T>submitAndAddQueueListener(ListeningExecutorService executorService, Callable<T> task, BlockingQueue<Future<T>> queue) Submits the task and adds a listener that adds the future toqueuewhen it completes.
-
Constructor Details
-
MoreExecutors
private MoreExecutors()
-
-
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
-
submitAndAddQueueListener
@GwtIncompatible private static <T> ListenableFuture<T> submitAndAddQueueListener(ListeningExecutorService executorService, Callable<T> task, BlockingQueue<Future<T>> queue) Submits the task and adds a listener that adds the future toqueuewhen it completes. -
platformThreadFactory
Returns a default thread factory used to create new threads.On AppEngine, returns
ThreadManager.currentRequestThreadFactory(). Otherwise, returnsExecutors.defaultThreadFactory().- Since:
- 14.0
-
isAppEngine
-
newThread
Creates a thread usingplatformThreadFactory(), and sets its name tonameunless changing the name is forbidden by the security manager. -
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
-
rejectionPropagatingExecutor
Returns an Executor that will propagateRejectedExecutionExceptionfrom the delegate executor to the givenfuture.Note, the returned executor can only be used once.
-