. . .
Example 12-9 Pipeline Design
processRequest_A() {
while(true) {, LENGTH);
resultA = processDataA(data);
processRequest_B() {
while(true) {
resultA = getFromQueueA();
resultB = processDataB(resultA);
os.write(resultB, LENGTH);
We can certainly see that this model would be valuable for simulations in which what you're
simulating is a pipeline. For other situations, it's not so clear. In silicon and on factory floors,
specialization is important. One section of a chip can execute only a single task (the instruction
fetch unit can only fetch instructions, never decode them), and it takes time for a worker to put
down a wrench and pick up a paintbrush.
This is not so for threads. It is actually easier and faster and the programming simpler for one
thread to execute an entire operation than to do a little work, package up the partial result, and
queue it for another thread. Although a number of programs that use this paradigm have been
suggested, it is not clear to us that any of them are superior to using one of the other designs.
Client/Server (Thread per Client)
The final model is also somewhat questionable to us. In this model, each client will have a thread
devoted to it, and that thread will remain inactive the vast majority of the time (Code Example 12-
10). The advantage of having a thread devoted to an individual client is that the thread can
maintain state for that client implicitly by what's on the stack and in thread-specific data. Although
this does save the programmer the effort of encapsulating that data, it's unclear that it's worth it
because of the large number of threads required. In POSIX we avoid doing this by having one
producer thread call select() on hundreds of sockets. In Java, this is not an option [there is
nothing similar to select() in Java; see Dealing with Many Open Sockets], so you are pretty
much forced to use this design.
Example 12-10 Thread per Client Design
public void startUp() throws Exception { // Executes in main thread
for (int i = 1; i < nConsumers; i++) {
Thread t = new Thread(new Consumer(workpile));
for (int i = 1; true; i++) {
socket = serverSocket.accept();
Thread t = new Thread(new Producer(workpile, socket));
System.out.println("Server[" + t.getName() +
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks