Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

LD220

macrumors member
Original poster
Jun 3, 2009
35
0
Hi guys. I'm currently taking an operating systems course and I'm attempting to write a program using Java threads for a class project. I've gone to the teacher and the TA with my problem and neither seems to know what's going wrong. I'm about to turn it in without using wait and notify, but I'm finding it really frustrating and would like to figure it out. Basically, we're simulating 10 threads as if they are commuting to work. There is a section where they travel over a bridge and anyone paying cash must first wait for the 5th commuter to travel through first. I have the threads wait, but when commuter5 notifies no one seems to receive the signal. What am I missing here? I've attached the two Java files and copy and pasted a bit below. Any help you could give me would be great! Thanks for your help!

private static int bridgeValue = 0;
...
public theCommuters(int number){
commuterNum = number; //commuterNum goes from 0 to numberOfCommuters-1
t = new Thread(this, "Commuter"+Integer.toString(number+1));
System.out.println("Time "+age()+": "+t.getName()+" has been created.");
t.start();
}
...
private void tollBridge(){
if (commuterNum == 4){ //commuterNum goes from 0 to numberOfCommuters-1
System.out.println("Time "+age()+": "+t.getName()+" is travelling over the "
+"bridge, but has an emergency! He can go first at the cash booth.");
payWithCash();
}

else if (getRand() <= 500){
payWithCash();
}

else{
try {
System.out.println("Time "+age()+": "+t.getName()+" is travelling over the "
+"bridge and has an EZ-Pass. No lines for him.");
Thread.sleep(getRand());
} catch (InterruptedException e){ }
}
}

private synchronized void payWithCash() {
if(commuterNum!=4){
System.out.println("Time "+age()+": "+t.getName()+" is travelling over the "
+"bridge and has to pay cash. He will have to wait.");
bridgeValue--;
if(bridgeValue < 0) {
while (true) {
try {
wait( );
break;
} catch(InterruptedException e){
if (bridgeValue >= 0)break;
else continue;
}
}
}
}

System.out.println("Time "+age()+": "+t.getName()+" paid the toll!");

bridgeValue++;
if (bridgeValue <= 0)
notify();
}
 
i don't think my full files posted yesterday, so here they are hopefully. i would really appreciate it if someone could take a look and tell me why when thread 5 does notify(), none of the threads that did wait() seem to receive the signal. thanks again!

marissa
 
Wait and notify act on a particular instance of an object (a particular commuter, in this case). From a cursory glance of your code, it looks like each object is calling wait on itself (this.wait(), or just wait() as you've written it) -- this means that until a second object calls notify on the first object (first.notify() if the first object is called first), the first object will never wake up. Moreover, once a thread T calls notify on itself (this.notify()), it will only wake up a thread waiting on T -- which none of the waiting threads are, since they're all waiting on themselves, not T.
 
Thanks so much for your reply! I was really stuck on this, but I've finally fixed it thanks to you. I just added a static commuter to the class and had them all wait and notify on that commuter. Now it seems to work.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.