CubeTwister 2.0alpha142 2012-02-11

ch.randelshofer.util
Class ConcurrentDispatcherAWT

java.lang.Object
  extended by ch.randelshofer.util.ConcurrentDispatcherAWT
All Implemented Interfaces:
Dispatcher, java.lang.Runnable

public class ConcurrentDispatcherAWT
extends java.lang.Object
implements Dispatcher, java.lang.Runnable

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 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.
 ConcurrentDispatcherAWT dispatcher = new ConcurrentDispatcherAWT();

 // 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:
1.1 2008-01-02 Added method join.
1.0 2001-12-31 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
ConcurrentDispatcherAWT()
          Creates a new ConcurrentDispatcherAWT 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.
ConcurrentDispatcherAWT(int priority, int threadMax)
          Creates a new ConcurrentDispatcherAWT.
 
Method Summary
 void dispatch(java.lang.Runnable runner)
          Enqueues the Runnable object, and executes it on a processor thread.
 void join()
           
 void run()
          This method is public as a side effect of the way how this class is implemented.
 void setMaxThreads(int threadMax)
          Sets the maximum number of concurrent threads.
 
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 int ENQUEUE_WHEN_BLOCKED
Set the policy to enqueue the runnable for later execution if there are no available threads in the pool.


RUN_WHEN_BLOCKED

public 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 Detail

ConcurrentDispatcherAWT

public ConcurrentDispatcherAWT()
Creates a new ConcurrentDispatcherAWT 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.


ConcurrentDispatcherAWT

public ConcurrentDispatcherAWT(int priority,
                               int threadMax)
Creates a new ConcurrentDispatcherAWT.

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

setMaxThreads

public void setMaxThreads(int threadMax)
Sets the maximum number of concurrent threads.

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

dispatch

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

Specified by:
dispatch in interface Dispatcher

run

public void run()
This method is public as a side effect of the way how this class is implemented. Do not call this method from outside this class.

This method dequeues all Runnable objects from the queue and executes them. The method returns when the queue is empty.

Specified by:
run in interface java.lang.Runnable

join

public void join()
          throws java.lang.InterruptedException
Specified by:
join in interface Dispatcher
Throws:
java.lang.InterruptedException

(c) Werner Randelshofer.
All rights reserved.