Class TestingExecutors
- java.lang.Object
-
- com.google.common.util.concurrent.testing.TestingExecutors
-
@GwtIncompatible public final class TestingExecutors extends Object
Factory methods forExecutorService
for testing.- Since:
- 14.0
- Author:
- Chris Nokleberg
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static com.google.common.util.concurrent.ListeningScheduledExecutorService
noOpScheduledExecutor()
Returns aScheduledExecutorService
that never executes anything.static com.google.common.util.concurrent.ListeningScheduledExecutorService
sameThreadScheduledExecutor()
Creates a scheduled executor service that runs each task in the thread that invokesexecute/submit/schedule
, as inThreadPoolExecutor.CallerRunsPolicy
.
-
-
-
Method Detail
-
noOpScheduledExecutor
public static com.google.common.util.concurrent.ListeningScheduledExecutorService noOpScheduledExecutor()
Returns aScheduledExecutorService
that never executes anything.The
shutdownNow
method of the returned executor always returns an empty list despite the fact that everything is still technically awaiting execution. ThegetDelay
method of anyScheduledFuture
returned by the executor will always return the max long value instead of the time until the user-specified delay.
-
sameThreadScheduledExecutor
@Beta public static com.google.common.util.concurrent.ListeningScheduledExecutorService sameThreadScheduledExecutor()
Creates a scheduled executor service that runs each task in the thread that invokesexecute/submit/schedule
, as inThreadPoolExecutor.CallerRunsPolicy
. This applies both to individually submitted tasks and to collections of tasks submitted viainvokeAll
,invokeAny
,schedule
,scheduleAtFixedRate
, andscheduleWithFixedDelay
. In the case of tasks submitted byinvokeAll
orinvokeAny
, 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).The returned executor is backed by the executor returned by
MoreExecutors.newDirectExecutorService()
and subject to the same constraints.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.Because of the nature of single-thread execution, the methods
scheduleAtFixedRate
andscheduleWithFixedDelay
are not supported by this class and will throw an UnsupportedOperationException.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:
- 32.0.0 (taking the place of a method with a different return type from 15.0)
-
-