Package org.jmock.lib.concurrent
Class DeterministicScheduler
- java.lang.Object
-
- org.jmock.lib.concurrent.DeterministicScheduler
-
- All Implemented Interfaces:
Executor,ExecutorService,ScheduledExecutorService
public class DeterministicScheduler extends Object implements ScheduledExecutorService
AScheduledExecutorServicethat executes commands on the thread that callsrunNextPendingCommand,runUntilIdleortick. Objects of this class can also be used asExecutors orExecutorServices if you just want to control background execution and don't need to schedule commands, but it may be simpler to use aDeterministicExecutor.- Author:
- nat
-
-
Constructor Summary
Constructors Constructor Description DeterministicScheduler()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanawaitTermination(long timeout, TimeUnit unit)voidexecute(Runnable command)<T> List<Future<T>>invokeAll(Collection<? extends Callable<T>> tasks)<T> List<Future<T>>invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)<T> TinvokeAny(Collection<? extends Callable<T>> tasks)<T> TinvokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)booleanisIdle()Reports whether scheduler is "idle": has no commands pending immediate execution.booleanisShutdown()booleanisTerminated()voidrunNextPendingCommand()Runs the next command scheduled to be executed immediately.voidrunUntilIdle()Runs all commands scheduled to be executed immediately but does not tick time forward.ScheduledFuture<?>schedule(Runnable command, long delay, TimeUnit unit)<V> ScheduledFuture<V>schedule(Callable<V> callable, long delay, TimeUnit unit)ScheduledFuture<?>scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)ScheduledFuture<?>scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)voidshutdown()List<Runnable>shutdownNow()Future<?>submit(Runnable command)<T> Future<T>submit(Runnable command, T result)<T> Future<T>submit(Callable<T> callable)voidtick(long duration, TimeUnit timeUnit)Runs time forwards by a given duration, executing any commands scheduled for execution during that time period, and any background tasks spawned by the scheduled tasks.
-
-
-
Method Detail
-
tick
public void tick(long duration, TimeUnit timeUnit)Runs time forwards by a given duration, executing any commands scheduled for execution during that time period, and any background tasks spawned by the scheduled tasks. Therefore, when a call to tick returns, the executor will be idle.- Parameters:
duration-timeUnit-
-
runUntilIdle
public void runUntilIdle()
Runs all commands scheduled to be executed immediately but does not tick time forward.
-
runNextPendingCommand
public void runNextPendingCommand()
Runs the next command scheduled to be executed immediately.
-
isIdle
public boolean isIdle()
Reports whether scheduler is "idle": has no commands pending immediate execution.- Returns:
- true if there are no commands pending immediate execution, false if there are commands pending immediate execution.
-
schedule
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
- Specified by:
schedulein interfaceScheduledExecutorService
-
schedule
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
- Specified by:
schedulein interfaceScheduledExecutorService
-
scheduleAtFixedRate
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
- Specified by:
scheduleAtFixedRatein interfaceScheduledExecutorService
-
scheduleWithFixedDelay
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
- Specified by:
scheduleWithFixedDelayin interfaceScheduledExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException- Specified by:
awaitTerminationin interfaceExecutorService- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
- Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
- Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
- Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdownin interfaceExecutorService
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminatedin interfaceExecutorService
-
shutdown
public void shutdown()
- Specified by:
shutdownin interfaceExecutorService
-
shutdownNow
public List<Runnable> shutdownNow()
- Specified by:
shutdownNowin interfaceExecutorService
-
submit
public <T> Future<T> submit(Callable<T> callable)
- Specified by:
submitin interfaceExecutorService
-
submit
public Future<?> submit(Runnable command)
- Specified by:
submitin interfaceExecutorService
-
submit
public <T> Future<T> submit(Runnable command, T result)
- Specified by:
submitin interfaceExecutorService
-
-