简体   繁体   中英

How to use wait and notify

I am creating a java application in which I am using three threads to perform three operations on a text file simultaneously.I have a toggle button in my application when I click start i am calling a method call() in which i am creating and starting all these threads and when i click stop i am calling a new method stopcall() in which i write a code to stop all these thread.

public void stopcall() throws Exception {
    System.out.println("hello stop call");

    synchronized(t) {   
        t.wait();   
    }

    synchronized(t1) {  
        t1.wait();
    }

    synchronized(t2) {  
        t2.wait(); 
    }

}

But stopcall() method is not working properly whenever i am calling this method my application hanged.I would be grateful if somebody help me and tell me how to use wait and notify in my application

Your application hangs because you are waiting for a locked object. wait() method hangs the thread until another thread uses notify() on that object.

You have to synchronize the method that accessing the shared object (the file in this case) to enable safe threading. here is an example using a boolean flag to indicate if the resource is currently in use or not.

if in use, the next thread will invoke wait() and will wait for a notification.

meantime when the 'currently using' thread will finish the synchronized block - it will invoke notifyAll() to alert all the waiting threads that the resource is free.

public class TestSync {
private boolean fileInUse = false;

public synchronized void syncWriting() {
    // blocks until a the file is free. if not - the thread will 'wait'. 
    // when notified : will do the while-loop again
    while (true) {
        if (!fileInUse){
            System.out.println("using the free file");
            fileInUse = true;
            //
            // code to write and close the file
            //
            notifyAll();
            return;
        }
        try {
            // wait if file in use. after being notified : 
            wait();
        } catch (InterruptedException e) {e.getMessage();}
    }
}

The wait() / notify() / notifyAll() methods are fairly easy to understand.

foo.wait() releases the lock on foo , and then it sleeps until foo is notified, and then in reacquires the lock before it returns.

foo.notifyAll() wakes up all threads that are sleeping in foo.wait() calls. If no threads are sleeping at the moment when it is called, then it does not do anything at all.

foo.notify() is the same as foo.notifyAll() except, it only picks one sleeping thread (if any) and wakes it.


The trick to using wait() and notify() is, suppose that you expect some thread A to wake up thread B with a foo.notify() . How do you guarantee that thread B will already be sleeping in a foo.wait() call before thread A calls foo.notify() ?

Remember: If the notify happens first, then it will be "lost". That is, the notify will do nothing, and the wait will never return.

That brings us to the reason why foo.wait() and foo.notify() are only allowed to be called from inside a synchronized(foo) block. You are supposed to use the synchronization, and some shared variable to prevent thread A from wait()ing for a notification that already has happened.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM