jconch.pipeline.impl
Class ExecutorThreadingModel

java.lang.Object
  extended by jconch.pipeline.impl.ExecutorThreadingModel
All Implemented Interfaces:
ThreadingModel

public class ExecutorThreadingModel
extends Object
implements ThreadingModel

A wrapper around ThreadingModel which allows the user to leverage the Executor API, and its myriad extensions. This class launches a seperate thread to monitor the pipeline stage it is wrapping and populate the executor, which means there can be multiple instances of this class in a pipeline which execute asynchronously.

This class is thread safe: the state may be modified safely during execution.

This class is somewhat optimized for a common case: finalize() will detect if a ThreadPoolExecutor is being used and will call ThreadPoolExecutor.shutdown() to enable an orderly shutdown.

View Source

Author:
Robert Fischer

Constructor Summary
ExecutorThreadingModel(Executor impl)
          Constructor.
 
Method Summary
 void execute(PipelineStage toRun)
          Delegates the execution of the argument to the underlying executor.
protected  void finalize()
           
 long getSpawnPeriod()
          Gets the period between executor calls for instance.
 void setSpawnDelay(long spawnDelay)
          Sets the period between executor calls for instance.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExecutorThreadingModel

public ExecutorThreadingModel(Executor impl)
Constructor. This uses the given implementation, and a default delay between spawns.

Parameters:
impl - The underlying implementation for execution.
Throws:
org.apache.commons.lang.NullArgumentException - If the argument is null.
Method Detail

execute

public void execute(PipelineStage toRun)
Delegates the execution of the argument to the underlying executor. Each call to PipelineStage.execute() is wrapped in its own Runnable that is passed to the Executor implementation provided.

Specified by:
execute in interface ThreadingModel
Parameters:
toRun - The stage to execute.
Throws:
org.apache.commons.lang.NullArgumentException - If the argument is null.

getSpawnPeriod

public long getSpawnPeriod()
Gets the period between executor calls for instance.

Returns:
the spawn period

setSpawnDelay

public void setSpawnDelay(long spawnDelay)
Sets the period between executor calls for instance. Values less than or equal to 0 are treated as 1.

Parameters:
spawnDelay - the new spawn period

finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable