jconch.testing
Class SerialExecutorService

java.lang.Object
  extended by jconch.testing.SerialExecutorService
All Implemented Interfaces:
Executor, ExecutorService

public class SerialExecutorService
extends Object
implements ExecutorService

This ExecutorService never spawns any new threads, it always executes everything it can immedietely. This Executor is not capable of being interrupted. This class is meant to be useful in unit tests.

Author:
Hamlet D'Arcy

Constructor Summary
SerialExecutorService()
           
 
Method Summary
 boolean awaitTermination(long timeout, TimeUnit unit)
          Returns immedietly.
 void execute(Runnable task)
          Executes the runnable immedietly on the current thread.
<T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks)
          Executes all the tasks on the current thread immedietly.
<T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
          Executes all the tasks on the current thread immedietly.
<T> T
invokeAny(Collection<? extends Callable<T>> tasks)
          Executes the tasks immedietly on the current thread, returning the first result available that didn't result in an exception.
<T> T
invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
          Executes the tasks immedietly on the current thread, returning the first result available that didn't result in an exception.
 boolean isShutdown()
           
 boolean isTerminated()
           
 void shutdown()
           
 List<Runnable> shutdownNow()
          Marks the executor as shutdown, and now more tasks are accepted for execution.
<T> Future<T>
submit(Callable<T> task)
          Executes the task immedietly on the current thread.
 Future<?> submit(Runnable task)
          Executes the task immedietly on the current thread.
<T> Future<T>
submit(Runnable task, T result)
          Executes the task immedietly on the current thread, returning the result passed in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.concurrent.ExecutorService
invokeAll, invokeAll, invokeAny, invokeAny
 

Constructor Detail

SerialExecutorService

public SerialExecutorService()
Method Detail

shutdown

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

shutdownNow

public List<Runnable> shutdownNow()
Marks the executor as shutdown, and now more tasks are accepted for execution.

Specified by:
shutdownNow in interface ExecutorService
Returns:
this ExecutorService is meant to be called from a single thread, so no Runnables are ever returned.

isShutdown

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

isTerminated

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

awaitTermination

public boolean awaitTermination(long timeout,
                                TimeUnit unit)
Returns immedietly.

Specified by:
awaitTermination in interface ExecutorService
Parameters:
timeout - ignored
unit - ignored
Returns:
always true

submit

public <T> Future<T> submit(Callable<T> task)
Executes the task immedietly on the current thread.

Specified by:
submit in interface ExecutorService
Parameters:
task - task to execute, may not be null
Returns:
the result of the task
Throws:
RuntimeException - if task execution failed

submit

public <T> Future<T> submit(Runnable task,
                            T result)
Executes the task immedietly on the current thread, returning the result passed in.

Specified by:
submit in interface ExecutorService
Parameters:
task - task to execute, may not be null
result - result to return in the future, may be null
Returns:
the result supplied as input

submit

public Future<?> submit(Runnable task)
Executes the task immedietly on the current thread.

Specified by:
submit in interface ExecutorService
Parameters:
task - task to execute
Returns:
a future that returns null

invokeAll

public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
                          throws InterruptedException
Executes all the tasks on the current thread immedietly. Cannot be interrupted.

Parameters:
tasks - tasks to execute
Returns:
all the futures
Throws:
InterruptedException

invokeAll

public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                                     long timeout,
                                     TimeUnit unit)
Executes all the tasks on the current thread immedietly. Cannot be interrupted.

Parameters:
tasks - tasks to execute
timeout - timeout length
unit - timeout time unit
Returns:
all the futures, some will be evaluated some will not depending on timeout

invokeAny

public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
            throws InterruptedException,
                   ExecutionException
Executes the tasks immedietly on the current thread, returning the first result available that didn't result in an exception. Cannot be interrupted. Cannot be interrupted.

Parameters:
tasks - tasks to execute, some of which will not get executed
Returns:
the first result available, other tasks will _not_ be evaluated
Throws:
ExecutionException - if no task ended successfully
InterruptedException

invokeAny

public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
                       long timeout,
                       TimeUnit unit)
            throws ExecutionException,
                   TimeoutException
Executes the tasks immedietly on the current thread, returning the first result available that didn't result in an exception. Cannot be interrupted.

Parameters:
tasks - tasks to execute, some of which will not get executed
timeout - timeout length
unit - timeout time unit
Returns:
the first result available, other tasks will _not_ be evaluated
Throws:
ExecutionException - if no task ended successfully
TimeoutException - if timeout occurs

execute

public void execute(Runnable task)
Executes the runnable immedietly on the current thread.

Specified by:
execute in interface Executor
Parameters:
task - task to execute