Quaqua 7.4.2 2011-07-05

ch.randelshofer.quaqua.util
Class ConcurrentDispatcher

java.lang.Object
  extended by ch.randelshofer.quaqua.util.ConcurrentDispatcher

public class ConcurrentDispatcher
extends java.lang.Object

Processes Runnable objects concurrently on a pool of processor threads. The order in which the runnable objects are processed is not necesseraly the same in which they were added to the dispatcher. There is one thread pool per instance.

Design pattern used: Acceptor Role in design pattern: EventCollector and EventProcessor

Example
The following program prints "Hello World" on the processor thread:

 // Create the Dispatcher.
 ConcurrentDispatcher dispatcher = new ConcurrentDispatcher();

 // Create the Runnable object.
 Runnable runner = new Runnable() {
     public void run()  {
         System.out.println("Hello World");
     }
 };

 // Execute the Runnable objekt using the dispatcher.
 dispatcher.dispatch(runner);
 

Version:
2.1 2009-06-01 Added dispose method.
2.0 2002-04-07 dispatchLIFO added.
1.0 2002-05-18 Created.
Author:
Werner Randelshofer, Hausmatt 10, Immensee, CH-6405, Switzerland

Field Summary
static int ENQUEUE_WHEN_BLOCKED
          Set the policy to enqueue the runnable for later execution if there are no available threads in the pool.
static int RUN_WHEN_BLOCKED
          Set the policy for blocked execution to be that the current thread executes the command if there are no available threads in the pool.
 
Constructor Summary
ConcurrentDispatcher()
          Creates a new ConcurrentDispatcher and sets the priority of the processor thread to java.lang.Thread.NORM_PRIORITY and with up to five concurrent threads in the thread pool.
ConcurrentDispatcher(int priority, int maxThreadCount)
          Creates a new ConcurrentDispatcher.
 
Method Summary
 void dispatch(java.lang.Runnable runner)
          Enqueues the Runnable object, and executes it on a processor thread.
 void dispatch(java.lang.Runnable runner, boolean isLIFO)
          Enqueues the Runnable object, and executes it on a processor thread.
 void dispose()
          Disposes the dispatcher and all associated processes.
 int getMaxThreadCount()
          Returns the maximal number of concurrent threads.
protected  void processEvents()
          This method dequeues all Runnable objects from the queue and executes them.
 void setMaxThreadCount(int maxThreadCount)
          Sets the maximum number of concurrent threads.
 void stop()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENQUEUE_WHEN_BLOCKED

public static final int ENQUEUE_WHEN_BLOCKED
Set the policy to enqueue the runnable for later execution if there are no available threads in the pool.

See Also:
Constant Field Values

RUN_WHEN_BLOCKED

public static final int RUN_WHEN_BLOCKED
Set the policy for blocked execution to be that the current thread executes the command if there are no available threads in the pool.

See Also:
Constant Field Values
Constructor Detail

ConcurrentDispatcher

public ConcurrentDispatcher()
Creates a new ConcurrentDispatcher and sets the priority of the processor thread to java.lang.Thread.NORM_PRIORITY and with up to five concurrent threads in the thread pool.


ConcurrentDispatcher

public ConcurrentDispatcher(int priority,
                            int maxThreadCount)
Creates a new ConcurrentDispatcher.

Parameters:
priority - The priority of the processor thread.
maxThreadCount - The maximal number of concurrent threads in the thread pool.
Method Detail

setMaxThreadCount

public void setMaxThreadCount(int maxThreadCount)
Sets the maximum number of concurrent threads.

Parameters:
maxThreadCount - Maximal number of concurrent threads. A value of zero or below zero stops the dispatcher when the queue is empty.

getMaxThreadCount

public int getMaxThreadCount()
Returns the maximal number of concurrent threads.


dispatch

public void dispatch(java.lang.Runnable runner)
Enqueues the Runnable object, and executes it on a processor thread.


dispatch

public void dispatch(java.lang.Runnable runner,
                     boolean isLIFO)
Enqueues the Runnable object, and executes it on a processor thread.


stop

public void stop()

processEvents

protected void processEvents()
This method dequeues all Runnable objects from the queue and executes them. The method returns when the queue is empty.


dispose

public void dispose()
Disposes the dispatcher and all associated processes.


Copyright 2003-2007 (c) Werner Randelshofer.
All rights reserved.