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();
}
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();
}