|
CubeTwister 2.0alpha142 2012-02-11 | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectch.randelshofer.util.PooledSequentialDispatcher
public class PooledSequentialDispatcher
Processes Runnable objects sequentially on a pool of processor threads. The order in which the runnable objects are processed is the same in which they were added to the dispatcher.
Per default, there is one pool per VM running 5 threads. You can
assign the dispatcher to a different thread pool using the constructor
or using method setThreadPool.
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 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:
PooledSequentialDispatcher.dispatchConcurrently(
new Runnable(public void run() { System.out.println("one"); });
);
PooledSequentialDispatcher.dispatchConcurrently(
new Runnable(public void run() { System.out.println("two"); });
);
PooledSequentialDispatcher.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:
PooledSequentialDispatcher dispatcher = new PooledSequentialDispatcher();
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.
| Constructor Summary | |
|---|---|
PooledSequentialDispatcher()
Creates a new PooledSequentialDispatcher which uses the global threadPool for dispatching its queue. |
|
| 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,
ConcurrentDispatcher pool)
Enqueues the Runnable object, and executes it sequentially on one of the processor threads of the specified 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. |
int |
getMaxThreadCount()
Retunrs the maximal number of concurrent threads. |
ConcurrentDispatcher |
getThreadPool()
Returns the underlying thread pool. |
void |
join()
|
void |
reassign()
(Re)starts the 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 |
setMaxThreadCount(int maxThreadCount)
Sets the maximum number of concurrent threads. |
void |
setThreadPool(ConcurrentDispatcher threadPool)
Assigns this dispatcher to the specified thread pool. |
void |
start()
Starts the event processor. |
void |
stop()
Stops the event processor and waits until it has finished. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public PooledSequentialDispatcher()
| Method Detail |
|---|
public void setMaxThreadCount(int maxThreadCount)
maxThreadCount - Maximal number of concurrent threads.
A value of zero or below zero stops the dispatcher
when the queue is empty.public int getMaxThreadCount()
public void setThreadPool(ConcurrentDispatcher threadPool)
public ConcurrentDispatcher getThreadPool()
public static void dispatchConcurrently(java.lang.Runnable runner)
runner - A runnable.public void dispatch(java.lang.Runnable runner)
dispatch in interface Dispatcherrunner - A runnable.
public void dispatch(java.lang.Runnable runner,
ConcurrentDispatcher pool)
runner - A runnable.reassign()public void reassign()
public void start()
public void stop()
public void run()
This method dequeues all Runnable objects from the queue and executes them. The method returns when the queue is empty.
run in interface java.lang.Runnable
public void join()
throws java.lang.InterruptedException
join in interface Dispatcherjava.lang.InterruptedException
|
(c) Werner Randelshofer. All rights reserved. |
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||