if (KILL)
new Thread(new Killer(120)).start();
acceptor = Thread.currentThread();
consumers = new Thread[nConsumers];
producers = new Thread[MAX_OPEN];
workpile = new Workpile(MAX_LENGTH);
for (int i = 0; i < nConsumers; i++) {
consumers[i] = new Thread(new Consumer(workpile));
consumers[i].start();
}
for (int i = 0; i < 3; i++) {
// Main start/stop loop
try {
serverSocket = new ServerSocket(port);
System.out.println("\n=========================================");
System.out.println("Server now listening on port " +
port);
nProducers = 0;
new Thread(new Stopper(workpile,
stopperTimeout)).start();
for (int j = 0; true; j++) {
// New client
loop
try {
socket = serverSocket.accept();
Client client = new Client(socket);
synchronized (this) {
waitIfTooManyClients();
}
Thread t = new Thread(new Producer(this,
client));
t.start();
producers[j] = t;
nProducers = j + 1;
System.out.println("Server[" + t.getName() +
"]\tStarted new client: " + client);
} catch (SocketException ie) {
synchronized (workpile) {
System.out.println("Acceptor " + ie);
Thread.interrupted();  //SocketException
does NOT clear flag!
if (workpile.stop)
break;
// stop better be true!
System.out.println("Impossible bug! Stop
must be true.");
System.exit(-1);
}
}
}
// End of new
client loop
serverSocket.close();
waitForOutstandingClients();
System.out.println("Server shutdown complete.");
InterruptibleThread.sleep(2000);  // "Feels" better
to delay here.
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks
Home