Results 1 to 4 of 4
  1. #1
    mityay is offline Member
    Join Date
    Jun 2009
    Posts
    8
    Rep Power
    0

    Default 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:
    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;
        }
    }
    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?
    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 appreciate
    Last edited by mityay; 01-04-2011 at 04:29 PM.

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    "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.

  3. #3
    yerrasrinu is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Smile

    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.

  4. #4
    mityay is offline Member
    Join Date
    Jun 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks everyone for your help! I had a false impression that notifyAll() will set all threads from waiting state no matter of what object the threads use. Now, I've understood, these threads must share common object to call wait() and notifyAll() on.

Similar Threads

  1. whats wrong with this wait() Notify all ??
    By Ultre in forum Threads and Synchronization
    Replies: 9
    Last Post: 06-25-2010, 04:35 PM
  2. Need help with wait() and notify()
    By Mkaveli in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-30-2010, 12:58 PM
  3. Wait() notify() implementation
    By georges in forum Advanced Java
    Replies: 4
    Last Post: 02-05-2010, 02:33 AM
  4. wait() and notify() trouble with UI
    By Atriamax in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-09-2009, 03:51 AM
  5. wait() and notify() problems
    By greyradio in forum Threads and Synchronization
    Replies: 1
    Last Post: 08-03-2009, 04: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
  •