Okay, the following was my attempt to do the programming project on threads on page 233 of the 2005 ed. of Heller and Roberts Complete
Java 2 Certification. (I know the answer's on the CD, but I got the book from the library and the CD was missing.) The basic point of the question was to have one Rendezvous and several Waiters (specified by command-line) all going to the hurry up and wait and get notified and come back at different times. (So you could see how the threads would return in different orders, presumably.) My program never gets past the first Waiter, for some reason. Little help? (Thanks!
class Rendezvous {
static int i;
synchronized void hurryUpAndWait() {
i++;
try {
wait();
} catch (InterruptedException ex) { }
}
}
class Waiter extends
Thread {
int waiterid;
Rendezvous rendewait = new Rendezvous();
Waiter(Rendezvous rende) {
waiterid += 1;
rende=rendewait;
run();
}
public void run() {
System.out.println("starting run for " +waiterid);
rendewait.hurryUpAndWait();
System.out.println("Notification has happened for Waiter ID #"+waiterid);
}
}
public class NotifyLab {
public static void main(
String[] args) {
int x = Integer.parseInt(args[0]);
Rendezvous rennot = new Rendezvous();
Waiter[] newwaiter = new Waiter[x];
while (x > 0) {
newwaiter[x] = new Waiter(rennot);
newwaiter[x].start();
System.out.println(x +"newwaiter was started.");
x--;
}
rennot.notifyAll();
}
}