Results 1 to 5 of 5
Thread: wait() and notify()
- 02-14-2011, 11:55 AM #1
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
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....
- 02-14-2011, 12:21 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-15-2011, 06:58 AM #3
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
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?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!");
}
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?
- 02-15-2011, 07:55 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-15-2011, 08:58 AM #5
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
Similar Threads
-
Need help with wait and notify
By mityay in forum Threads and SynchronizationReplies: 3Last Post: 01-06-2011, 04:24 PM -
Need help with wait() and notify()
By Mkaveli in forum Threads and SynchronizationReplies: 2Last Post: 03-30-2010, 11:58 AM -
Wait() notify() implementation
By georges in forum Advanced JavaReplies: 4Last Post: 02-05-2010, 01:33 AM -
wait() and notify() trouble with UI
By Atriamax in forum Threads and SynchronizationReplies: 2Last Post: 12-09-2009, 02:51 AM -
wait() and notify() problems
By greyradio in forum Threads and SynchronizationReplies: 1Last Post: 08-03-2009, 03:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks