org.jmock.lib.concurrent
Class DeterministicScheduler

java.lang.Object
  extended by org.jmock.lib.concurrent.DeterministicScheduler
All Implemented Interfaces:
Executor, ExecutorService, ScheduledExecutorService

public class DeterministicScheduler
extends Object
implements ScheduledExecutorService

A ScheduledExecutorService that executes commands on the thread that calls runNextPendingCommand, runUntilIdle or tick. Objects of this class can also be used as Executors or ExecutorServices if you just want to control background execution and don't need to schedule commands, but it may be simpler to use a DeterministicExecutor.

Author:
nat

Constructor Summary
DeterministicScheduler()
           
 
Method Summary
 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.
<V> ScheduledFuture<V>
schedule(Callable<V> callable, long delay, TimeUnit unit)
           
 ScheduledFuture<?> schedule(Runnable command, 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()
           
<T> Future<T>
submit(Callable<T> callable)
           
 Future<?> submit(Runnable command)
           
<T> Future<T>
submit(Runnable command, T result)
           
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DeterministicScheduler

public DeterministicScheduler()
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.

execute

public void execute(Runnable command)
Specified by:
execute in interface Executor

schedule

public ScheduledFuture<?> schedule(Runnable command,
                                   long delay,
                                   TimeUnit unit)
Specified by:
schedule in interface ScheduledExecutorService

schedule

public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                                       long delay,
                                       TimeUnit unit)
Specified by:
schedule in interface ScheduledExecutorService

scheduleAtFixedRate

public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                              long initialDelay,
                                              long period,
                                              TimeUnit unit)
Specified by:
scheduleAtFixedRate in interface ScheduledExecutorService

scheduleWithFixedDelay

public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                 long initialDelay,
                                                 long delay,
                                                 TimeUnit unit)
Specified by:
scheduleWithFixedDelay in interface ScheduledExecutorService

awaitTermination

public boolean awaitTermination(long timeout,
                                TimeUnit unit)
                         throws InterruptedException
Specified by:
awaitTermination in interface ExecutorService
Throws:
InterruptedException

invokeAll

public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
                          throws InterruptedException
Specified by:
invokeAll in interface ExecutorService
Throws:
InterruptedException

invokeAll

public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                                     long timeout,
                                     TimeUnit unit)
                          throws InterruptedException
Specified by:
invokeAll in interface ExecutorService
Throws:
InterruptedException

invokeAny

public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
            throws InterruptedException,
                   ExecutionException
Specified by:
invokeAny in interface ExecutorService
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 interface ExecutorService
Throws:
InterruptedException
ExecutionException
TimeoutException

isShutdown

public boolean isShutdown()
Specified by:
isShutdown in interface ExecutorService

isTerminated

public boolean isTerminated()
Specified by:
isTerminated in interface ExecutorService

shutdown

public void shutdown()
Specified by:
shutdown in interface ExecutorService

shutdownNow

public List<Runnable> shutdownNow()
Specified by:
shutdownNow in interface ExecutorService

submit

public <T> Future<T> submit(Callable<T> callable)
Specified by:
submit in interface ExecutorService

submit

public Future<?> submit(Runnable command)
Specified by:
submit in interface ExecutorService

submit

public <T> Future<T> submit(Runnable command,
                            T result)
Specified by:
submit in interface ExecutorService