A Workpile is a container for a list of Requests and the
synchronization
variables that protect its internals. The synchronization and
management
of the list is EXTERNAL to the class because I want to illustrate
its
use in the producer/consumer code (and to make this program as
similar
as possible to the C version).
The Workpile is constructed on top of a List (see Extensions). It
could
equally well be implemented by subclassing Vector; unfortunately,
Vector
is HORRIBLY inefficient for lists.
*/
import java.io.*;
import Extensions.*;
public class Workpile {
List
list = List.nil;
int
length = 0;
static int
max = 10;
Mutex
mutex = new Mutex();
ConditionVar producerCV = new ConditionVar();
ConditionVar consumerCV = new ConditionVar();
boolean
stop = false;
public Workpile(int i) {
max = i;
}
public void add(Request request) {
list = list.cons(request);
length++;
}
public Request remove() {
Request request = (Request) list.first;
list = list.next;
length--;
return request;
}
public boolean empty() {
return length == 0;
}
public boolean full() {
return length == max;
}
}
Now a little problem we've glossed over. You may have noticed that our program has no way to
tell if it has sent out all the pending replies before the "End" request comes across. It is possible
that the client program takes care of this, though ours doesn't. Obviously, this must be done to
have a properly running program. Lots of techniques are possible, none of which are uniquely
outstanding. We made a couple of minor additions to the server which allow it to keep track of the
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks
Home