Results 1 to 4 of 4
Thread: Need help with wait and notify
- 01-04-2011, 05:15 AM #1
Member
- Join Date
- Jun 2009
- Posts
- 8
- Rep Power
- 0
Need help with wait and notify
I have attempted to implement an example of guarded blocks I've obtained at Guarded Blocks (The Java™ Tutorials > Essential Classes > Concurrency) The following code was generated:
I would expect guard thread to get notified by notifier thread and exit the wait(). However, this does not occur. guard thread remains in waiting state. Any idea why?Java Code:public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { MyBoolean joy = new MyBoolean(); new MyThread("guard", joy).start(); new MyThread("notifier", joy).start(); } } class MyThread extends Thread { private MyBoolean joy; private String type; public MyThread(String type, MyBoolean joy) { this.type = type; this.joy = joy; } public synchronized void guardedJoy() { System.out.println("Inside guardedJoy"); while(!joy.isJoy()) { // synchronized(joy) // { try { System.out.println("joy = " + joy.isJoy()); wait(); // this wait is never passed even though notifyAll() is called in another thread // joy.wait(); } catch (InterruptedException e) {e.printStackTrace();} // } } System.out.println("Joy and efficiency have been achieved!"); } public synchronized void notifyJoy() { System.out.println("Inside notifyJoy"); joy.setJoy(true); System.out.println("joy = " + joy.isJoy()); // synchronized(joy) // { notifyAll(); // calling this notify does not stop the other thread from waiting // joy.notifyAll(); // } System.out.println("Notified"); } @Override public void run() { if (type.equals("guard")) guardedJoy(); if (type.equals("notifier")) notifyJoy(); } } class MyBoolean { private boolean joy; public void setJoy(boolean joy) { this.joy = joy; } public boolean isJoy() { return joy; } public MyBoolean() { this.joy = false; } }
Interestingly, if wait() and notify() are synchronized with the object they share (joy), it works perfectly (this code is commented out). Is this the only resolution?
Greatly appreciateLast edited by mityay; 01-04-2011 at 03:29 PM.
- 01-05-2011, 05:07 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
"notifyAll" notifies all threads waiting on the object that it is called on. In your code, the two threads are each waiting on their own 'this' ('wait' within a subclass of Thread waits on the object associated with that particular thread instance).
Try uncommenting 'joy.wait', and 'joy.notifyAll', as well as the 'synchronized(joy)' blocks.
- 01-05-2011, 01:03 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 1
- Rep Power
- 0
As explained by one of the senior member previously, you are waiting on one monitor/object and notifying on the other monitor/object. In case of joy, it will work because it is the same object you are waiting and notifying. However, there is little chance for joy not to work due to sequencing/ordering issue where notifyAll could happen before wait.
- 01-06-2011, 04:24 PM #4
Member
- Join Date
- Jun 2009
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
whats wrong with this wait() Notify all ??
By Ultre in forum Threads and SynchronizationReplies: 9Last Post: 06-25-2010, 03:35 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