org.d_haven.event.command
Class ProcessorBoundThreadPolicy

java.lang.Object
  extended by org.d_haven.event.command.AbstractThreadPolicy
      extended by org.d_haven.event.command.ProcessorBoundThreadPolicy
All Implemented Interfaces:
java.lang.Runnable, ThreadPolicy

public class ProcessorBoundThreadPolicy
extends AbstractThreadPolicy

The ProcessorBoundThreadPolicy is a ThreadPolicy designed to process pipelines in a number of threads that is a multiple of the number of threads in the system. This implementation requires that you have the Runtime.getRuntime().getAvailableProcessors() method introduced in Java 1.4. If you want to use the event system on an older JVM, do not use this class.


Field Summary
private static long DEFAULT_WAIT_TIME
           
private  EDU.oswego.cs.dl.util.concurrent.PooledExecutor m_executor
           
private  int m_numThreads
           
private  long m_waitTime
           
 
Constructor Summary
ProcessorBoundThreadPolicy()
          Create a new ProcessorBoundThreadPolicy with one thread per processor and a default wait time of one second between runs.
ProcessorBoundThreadPolicy(int multiplier)
          Create a new ProcessorBoundThreadPolicy with the number of threads equal to the number of processor multiplied by the supplied multiplier.
ProcessorBoundThreadPolicy(int multiplier, long waitTime)
          Create a new ProcessorBoundThreadPolicy with the number of threads equal to the number of processor multiplied by the supplied multiplier.
 
Method Summary
 EventPipelineRunner createRunner(EventPipeline pipeline)
          Create the EventPipelineRunner that will be used to get events from the Sources to the EventHandler.
protected  void endLoop()
          Provide a hook to do further cleanup when we end the management loop.
 int getNumThreads()
          Get the number of threads to use for running the pipelines.
 EDU.oswego.cs.dl.util.concurrent.ThreadFactory getThreadFactory()
          Get the ThreadFactory used to create new threads.
 long getWaitTime()
          Get the time we wait between runs of the pipeline.
protected  void process(java.util.Collection pipelines)
          Do the actual management logic that needs to be done in each time through the main loop.
 void setThreadFactory(EDU.oswego.cs.dl.util.concurrent.ThreadFactory factory)
          Set the ThreadFactory for the policy to use.
protected  void startLoop()
          Provide a hook to do further initialization when we start the management loop.
 java.lang.String toString()
           
 
Methods inherited from class org.d_haven.event.command.AbstractThreadPolicy
executeLoop, isRunning, run, terminateLoop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_WAIT_TIME

private static final long DEFAULT_WAIT_TIME
See Also:
Constant Field Values

m_executor

private final EDU.oswego.cs.dl.util.concurrent.PooledExecutor m_executor

m_waitTime

private final long m_waitTime

m_numThreads

private final int m_numThreads
Constructor Detail

ProcessorBoundThreadPolicy

public ProcessorBoundThreadPolicy()
Create a new ProcessorBoundThreadPolicy with one thread per processor and a default wait time of one second between runs.


ProcessorBoundThreadPolicy

public ProcessorBoundThreadPolicy(int multiplier)
Create a new ProcessorBoundThreadPolicy with the number of threads equal to the number of processor multiplied by the supplied multiplier. It also uses the default wait time of one second between runs.

Parameters:
multiplier - the multiplier against the number of threads
Throws:
java.lang.IllegalArgumentException - if the multiplier is less than 1

ProcessorBoundThreadPolicy

public ProcessorBoundThreadPolicy(int multiplier,
                                  long waitTime)
Create a new ProcessorBoundThreadPolicy with the number of threads equal to the number of processor multiplied by the supplied multiplier. It also uses the supplied wait time for use between runs.

Parameters:
multiplier - the multiplier against the number of threads
waitTime - the time to wait between runs
Throws:
java.lang.IllegalArgumentException - if the multiplier or waitTime is less than 1
Method Detail

getWaitTime

public long getWaitTime()
Get the time we wait between runs of the pipeline.

Returns:
the wait time in ms

getNumThreads

public int getNumThreads()
Get the number of threads to use for running the pipelines.

Returns:
the number of threads

setThreadFactory

public void setThreadFactory(EDU.oswego.cs.dl.util.concurrent.ThreadFactory factory)
Description copied from interface: ThreadPolicy
Set the ThreadFactory for the policy to use. All new threads created after this is set will use the supplied factory.

Parameters:
factory - the factory to use

createRunner

public EventPipelineRunner createRunner(EventPipeline pipeline)
Description copied from interface: ThreadPolicy
Create the EventPipelineRunner that will be used to get events from the Sources to the EventHandler.

Parameters:
pipeline - the pipeline to run
Returns:
the EventPipelineRunner

process

protected void process(java.util.Collection pipelines)
                throws java.lang.Exception
Description copied from class: AbstractThreadPolicy
Do the actual management logic that needs to be done in each time through the main loop.

Specified by:
process in class AbstractThreadPolicy
Parameters:
pipelines - The pipelines to manage
Throws:
java.lang.Exception - if there is a problem or the thread is interrupted

getThreadFactory

public EDU.oswego.cs.dl.util.concurrent.ThreadFactory getThreadFactory()
Get the ThreadFactory used to create new threads.

Returns:
the thread factory

startLoop

protected void startLoop()
                  throws java.lang.InterruptedException
Description copied from class: AbstractThreadPolicy
Provide a hook to do further initialization when we start the management loop. It is very important to remember to actually start the AbstractThreadPolicy.run() method. This object is Runnable so you can pass it in to a new thread.

Specified by:
startLoop in class AbstractThreadPolicy
Throws:
java.lang.InterruptedException - if the calling thread is interrupted

endLoop

protected void endLoop()
Description copied from class: AbstractThreadPolicy
Provide a hook to do further cleanup when we end the management loop.

Overrides:
endLoop in class AbstractThreadPolicy

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object