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
AScheduledExecutorService
that executes commands on the thread that callsrunNextPendingCommand
,runUntilIdle
ortick
. Objects of this class can also be used asExecutor
s orExecutorService
s 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 boolean
awaitTermination(long timeout, TimeUnit unit)
void
execute(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> T
invokeAny(Collection<? extends Callable<T>> tasks)
<T> T
invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
boolean
isIdle()
Reports whether scheduler is "idle": has no commands pending immediate execution.boolean
isShutdown()
boolean
isTerminated()
void
runNextPendingCommand()
Runs the next command scheduled to be executed immediately.void
runUntilIdle()
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)
void
shutdown()
List<Runnable>
shutdownNow()
Future<?>
submit(Runnable command)
<T> Future<T>
submit(Runnable command, T result)
<T> Future<T>
submit(Callable<T> callable)
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.
-
-
-
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:
schedule
in interfaceScheduledExecutorService
-
schedule
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
- Specified by:
schedule
in interfaceScheduledExecutorService
-
scheduleAtFixedRate
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
- Specified by:
scheduleAtFixedRate
in interfaceScheduledExecutorService
-
scheduleWithFixedDelay
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
- Specified by:
scheduleWithFixedDelay
in interfaceScheduledExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
awaitTermination
in interfaceExecutorService
- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
- Specified by:
invokeAll
in interfaceExecutorService
- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
invokeAll
in interfaceExecutorService
- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
- Specified by:
invokeAny
in interfaceExecutorService
- Throws:
InterruptedException
ExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
- Specified by:
invokeAny
in interfaceExecutorService
- Throws:
InterruptedException
ExecutionException
TimeoutException
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdown
in interfaceExecutorService
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminated
in interfaceExecutorService
-
shutdown
public void shutdown()
- Specified by:
shutdown
in interfaceExecutorService
-
shutdownNow
public List<Runnable> shutdownNow()
- Specified by:
shutdownNow
in interfaceExecutorService
-
submit
public <T> Future<T> submit(Callable<T> callable)
- Specified by:
submit
in interfaceExecutorService
-
submit
public Future<?> submit(Runnable command)
- Specified by:
submit
in interfaceExecutorService
-
submit
public <T> Future<T> submit(Runnable command, T result)
- Specified by:
submit
in interfaceExecutorService
-
-