Programming Java threads in the real world, Part 9
More threads in an object-oriented world: Synchronous dispatchers, active objects, detangling console I/O
By Allen Holub, JavaWorld.com, 06/01/99
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
This month I'm picking up the architectural theme from the
May 1999 Java Toolbox column, with a look at two additional architectural solutions to thread-synchronization problems. You'll remember from last
month that object-oriented systems are designed in terms of synchronous and asynchronous messages, not in terms of threads
of execution. (If you don't remember that, you should read the previous article.) The two strategies I presented for implementing
asynchronous messages (the one-thread-per-message and thread-pool techniques) work just fine in many applications, but since
multiple threads are running concurrently -- perhaps accessing the same object -- you still have to worry about interthread
synchronization. This month I'll look at two additional approaches that can all but eliminate the need for synchronization
between messages.
Synchronous dispatching
A synchronous dispatcher, or round-robin scheduler, solves the synchronization problem by simulating multithreading within a single Java thread. Let's
start out by considering two tasks that need to be executed in parallel:

Figure 1. Two tasks to be executed in parallel
Each of these tasks naturally divides into four chunks, and each could be executed on its own thread. Let's also imagine that
the four chunks have to be atomic (they cannot tolerate interruption while they're executing), but that it's not a problem
if the task is preempted between chunks. The only way to get this atomicity in a normal preemptive multitasking environment
is to synchronize the operations, with all the concomitant overhead and complexity.
To move from a single task divided into chunks to a synchronous dispatcher, imagine that we can break up the single task into
four independent tasks, as illustrated in Figure 2.

Figure 2. A single task broken out into four independent tasks
It isn't too hard to imagine how we could implement the chunks; you could define each chunk as the run() method of a Runnable object, for example, put the objects into an array, and then write a simple scheduler to execute the objects one at a time
with a sleep() or yield() between chunks:
Runnable[] first_task = new Runnable[]
{
new Runnable(){ public void run(){ /* do chunk 1 here */ } },
new Runnable(){ public void run(){ /* do chunk 2 here */ } },
new Runnable(){ public void run(){ /* do chunk 3 here */ } },
new Runnable(){ public void run(){ /* do chunk 4 here */ } },
};
for( int i = 0; i < first_task.length; ++i )
{ first_task[i].run();
Thread.getCurrentThread().yield();
}
Doug Schmidt coined the term Reactor for this design pattern (see Resources). The Reactor pattern emerged from Schmidt's work on the ACE framework (see Resources), as a way to accumulate various operations that should occur when a given event is triggered. The event handler then executes
the for loop. The effect is essentially the same as several threads waiting on a single condition variable that's set true by the
event. However, unlike a condition variable, you have control over both the sequence of execution and the moment at which
you give up that control.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- The "Articles" section of Allen's Web site contains links to all previous articles in this series, including downloadable
versions of all the code
http://www.holub.com
- A description of the Addison Wesley Design Pattern series
http://www.awl.com/cseng/sps/index.html
- "ReactorAn Object Behavioral Pattern for Concurrent Event Demultiplexing and Event Handler Dispatching," Doug Schmidt; Pattern Languages of Program Design, James Coplien and Doug Schmidt (Addison-Wesley, 1995)
http://www1.fatbrain.com/asp/bookinfo/bookinfo.asp?theisbn=0201607344
- "Active ObjectAn Object Behavioral pattern for Concurrent Programming," R.G.Lavender and Doug Schmidt; Pattern Languages of Program Design 2, John Vlissides, Doug Coplien, and Mormon Kerth (Addison-Wesley, 1995)
http://www1.fatbrain.com/asp/bookinfo/bookinfo.asp?theisbn=0201895277
- Doug Schmidt's home page contains a description of ACE (the Adaptive Communication Environment) -- a class framework for developing
concurrent networked applications in an object-oriented way. Links to TAO, a CORBA object-request broker built with ACE, can
also be found here
http://www.cs.wustl.edu/~schmidt/ACE.html
- Design Patterns Elements of Reusable Object-Oriented Software, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (Addison Wesley, 1994). The Command design pattern is presented.
This book is essential reading for any object-oriented designer
http://www1.fatbrain.com/asp/bookinfo/bookinfo.asp?theisbn=0201633612
- Taming Java Threads, Allen Holub's book on Java threads, is forthcoming from Apress
http://www.apress.com