Search code examples
javamultithreadingwaitnotify

Threads notify not working after wait problem - java


I cannot find The Problem Can Someone Help me.

public class Achterbahn {

private final Object monitor = new Object();

public   synchronized void test() throws InterruptedException {
    
    
        //monitor.wait();
        

        System.out.println("car");
        wait();
        System.out.println("car");
    
}

public  synchronized void Passagier() throws InterruptedException {
    Thread.sleep(2000);

    
        System.out.println("p");

        notify();
    
    //b.t1.notify();
    
    
}
public static void main(String []args) throws InterruptedException {
    

    Thread t4 = new Thread(new Runnable() {

        @Override
        public void run() {
            Achterbahn b = new Achterbahn();
            try {
                b.Passagier();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
    });
    Thread t5= new Thread(new Runnable() {
        
        @Override
        public void run() {
            Achterbahn b = new Achterbahn();
            try {
                b.test();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
    
    });
    
    
    new Thread(t4).start();
    new Thread(t5).start();     
      t5.join();
      t4.join();
    
}


   }

The output is: car p

it seems like notify is working i want print also car at the last but i donot konw why its not working

i hope Someone Help me. as soon as possible.

i have all methodes in the same class and i tried also sepreate classes but it didnt work


Solution

  • (I am guessing in this case that “it didn’t work” means the program hangs. Please be specific about what the issue you’re seeing is.)

    There are 2 issues. One is that you are creating separate objects in each thread. The object that wait and notify are called on have to be the same, the monitor that is waited on is the one that needs to receive the notify. In this code the synchronized methods use the intrinsic lock on the instance that the methods are called on.

    Create the object once in the main method, each thread needs to reference the same object.

    The second issue, once you fix the first issue, will be a race condition. If the notify performed by one thread occurs first then when the wait executes the notify has already happened and the wait keeps waiting forever.

    Add a condition variable to remember whether the notify occurred.

    In general the pattern is to check the condition in a loop, see this question: Why we must use "while" for checking race condition not "if". The post has an example of using a variable to see if a condition occurred, here it

    synchronized(obj)
    {
        while (condition_not_matched)
        {
            obj.wait();
        }
        //continue
        dosomething();
    }