Java wait() and notify() problems

Discussion in 'Mac Programming' started by LD220, Jul 22, 2009.

  1. macrumors member

    Joined:
    Jun 3, 2009
    #1
    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();
    }
     
  2. thread starter macrumors member

    Joined:
    Jun 3, 2009
    #2
    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
     
  3. macrumors 6502

    Joined:
    Jun 23, 2003
    #3
    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.
     
  4. thread starter macrumors member

    Joined:
    Jun 3, 2009
    #4
    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.
     

Share This Page