Results 1 to 5 of 5
  1. #1
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default wait() and notify()

    hi friends

    i have a doubt on wait(). i learned that on invoking wait() method loss its lock on object and wait untill a notify() or notifyAll() is invoked. but when i tried it works fine without notify(). when the thread on which we are waiting is finished, waiting thread is executed even without notify().

    Java Code:
    class Thread1 implements Runnable
    {
    public void run()
    	{
    	System.out.println("Hai from Thread1");
    	}
    }
    
    class Wait_Notify
    {
    public static void main(String ... args)
    	{
    	System.out.println("main Begins");
    	Thread t = new Thread(new Thread1());
    	t.start();
    	try
    	{
    	synchronized(t){
    		t.wait();
    		}
    	}
    	catch(InterruptedException e)
    	{
    	}
    	System.out.println("Main ends");
    	}
    }

    if so what is the difference between wait() and join() in this case?

    thanks in advance....

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jomypgeorge View Post
    if so what is the difference between wait() and join() in this case?
    If your waiting thread wakes up and starts running again without a notify(All) issued by another thread that wakeup is a spurious one; this is what the API documentation has to say about it:

    Quote Originally Posted by API Object.wait
    A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:

    Java Code:
         synchronized (obj) {
             while (<condition does not hold>)
                 obj.wait(timeout);
             ... // Perform action appropriate to condition
         }
    (For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks josAH, one more doubt now...

    when i read java doc it says wait should be in a loop
    like

    public synchronized guardedJoy() {
    //This guard only loops once for each special event, which may not
    //be the event we're waiting for.
    while(!joy) {
    try {
    wait();
    } catch (InterruptedException e) {}
    }
    System.out.println("Joy and efficiency have been achieved!");
    }
    why should we want to place wait in a loop?
    isn't one wait() is enough to wait until notify() is called?

    or is this means some thread may call notify() without making joy = true?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jomypgeorge View Post
    thanks josAH, one more doubt now...

    when i read java doc it says wait should be in a loop
    like



    why should we want to place wait in a loop?
    isn't one wait() is enough to wait until notify() is called?

    or is this means some thread may call notify() without making joy = true?
    It's just because of those spurious wakeups: your threads play the 'game' by checking a condition and wait if the condition is false; you assume that another thread notifies your waiting thread after ensuring that the condition is true. Not so with a spurious wakeup: your waiting thread will be woken up without that condition being true. Therefor the woken up thread should test whether or not it has to wait again, i.e. the condition test should be put in a loop. Don't do anything else in that loop: while the condition isn't true, wait.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

Similar Threads

  1. Need help with wait and notify
    By mityay in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-06-2011, 04:24 PM
  2. Need help with wait() and notify()
    By Mkaveli in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-30-2010, 11:58 AM
  3. Wait() notify() implementation
    By georges in forum Advanced Java
    Replies: 4
    Last Post: 02-05-2010, 01:33 AM
  4. wait() and notify() trouble with UI
    By Atriamax in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-09-2009, 02:51 AM
  5. wait() and notify() problems
    By greyradio in forum Threads and Synchronization
    Replies: 1
    Last Post: 08-03-2009, 03:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •