CubeTwister 2.0alpha142 2012-02-11

ch.randelshofer.util
Class PooledSequentialDispatcherAWT

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

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

Dispatches Runnable objects sequentially or concurrently on a pool of processor threads. There is one pool per VM.

The static method dispatchConcurrently enqueues the runnable object for concurrent execution. That is, it will be processed as soon as one of the pooled threads becomes available.

The instance method dispatch enqueues the runnable object for sequential execution. It will be processed by one of the pooled threads when all of the preceedingly enqueued runnables have been processed. There is one queue per PooledSequentialDispatcherAWT instance.

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

Example 1
The following program prints "one", "two", "three" on concurrent processor threads:

 PooledSequentialDispatcherAWT.dispatchConcurrently(
     new Runnable(public void run() { System.out.println("one"); });
 );
 PooledSequentialDispatcherAWT.dispatchConcurrently(
     new Runnable(public void run() { System.out.println("two"); });
 );
 PooledSequentialDispatcherAWT.dispatchConcurrently(
     new Runnable(public void run() { System.out.println("three"); });
 );
 
The order of the output is not granted, since the runnables are executed concurrently. It could be "one","two","three" or "three","one","two" or any other possible combination. Even intermingled output is possible.

Example 2
The following program prints "one", "two", "three" on sequential processor threads:

 PooledSequentialDispatcherAWT dispatcher = new PooledSequentialDispatcherAWT();

 dispatcher.dispatch(
     new Runnable(public void run() { System.out.println("one"); });
 );
 dispatcher.dispatch(
     new Runnable(public void run() { System.out.println("two"); });
 );
 dispatcher.dispatch(
     new Runnable(public void run() { System.out.println("three"); });
 );
 
Since all runnables are dispatched by the same PoolDispatcherAWT instance, it is granted, that they will be executed in the same order as they were added to the queue. It is also granted, that the output will not be intermingled, because a runnable will be executed only, when its predecessor has finished.

Version:
1.2 2008-01-02 Added method join.
1.1 2003-05-03 Method stop invoked wait() on a different object than the one specified in the synchronize statement.
1.0.1 2001-12-31 Comments translated into english.
1.0 2001-12-27 Created.
Author:
Werner Randelshofer, Hausmatt 10, Immensee, CH-6405, Switzerland

Constructor Summary
PooledSequentialDispatcherAWT()
          Creates a new PooledSequentialDispatcherAWT and sets the priority of the processor thread to java.lang.Thread.NORM_PRIORITY.
 
Method Summary
 void dispatch(java.lang.Runnable runner)
          Enqueues the Runnable object, and executes it sequentially on one of the processor threads of the thread pool associated with this class.
 void dispatch(java.lang.Runnable runner, ConcurrentDispatcherAWT pool)
          Enqueues the Runnable object, and executes it sequentially on one of the processor threads of the given thread pool or - if there is already a thread associated with the queue - on that thread.
static void dispatchConcurrently(java.lang.Runnable runner)
          Enqueues the Runnable object, and executes it concurrently by one of the processor threads.
 void join()
           
 void reassign()
          Reassigns the queue to the thread pool provided by this class.
 void run()
          This method is public as a side effect of the implementation of this class.
 void stop()
          Stops the event processor and wait until it has finished.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PooledSequentialDispatcherAWT

public PooledSequentialDispatcherAWT()
Creates a new PooledSequentialDispatcherAWT and sets the priority of the processor thread to java.lang.Thread.NORM_PRIORITY.

Method Detail

dispatchConcurrently

public static void dispatchConcurrently(java.lang.Runnable runner)
Enqueues the Runnable object, and executes it concurrently by one of the processor threads. The runnables are not necesseraly executed in the same order as they were enqueued.

Parameters:
runner - A runnable.

dispatch

public void dispatch(java.lang.Runnable runner)
Enqueues the Runnable object, and executes it sequentially on one of the processor threads of the thread pool associated with this class. The runnables are executed in the same order as they were enqueued.

Specified by:
dispatch in interface Dispatcher
Parameters:
runner - A runnable.

dispatch

public void dispatch(java.lang.Runnable runner,
                     ConcurrentDispatcherAWT pool)
Enqueues the Runnable object, and executes it sequentially on one of the processor threads of the given thread pool or - if there is already a thread associated with the queue - on that thread. The runnables are executed in the same order as they were enqueued.

Parameters:
runner - A runnable.

reassign

public void reassign()
Reassigns the queue to the thread pool provided by this class.


stop

public void stop()
Stops the event processor and wait until it has finished.


run

public void run()
This method is public as a side effect of the implementation of this class. 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.