Results 1 to 3 of 3
  1. #1
    gunitinug is offline Member
    Join Date
    Dec 2016
    Posts
    13
    Rep Power
    0

    Default Wait and NotifyAll

    This code hangs forever without outputting anything.

    Java Code:
    public class JoyTest {
      boolean joy=false;
      
      public synchronized void guardedJoy() {
        while(!joy) {
          try {
            wait();
          } catch(Exception e){}
          System.out.println("Joy has been achieved!");
        }
      }
      
      public synchronized void notifyJoy() {
        joy=true;
        notifyAll();
      }
      
      public static void main(String[] args) {
        JoyTest jt = new JoyTest();
        jt.guardedJoy();
        jt.notifyJoy();
      }
    }
    Do I have to create a Runnable class that uses JoyTest methods then start a thread?
    Last edited by gunitinug; 04-18-2017 at 07:58 AM.

  2. #2
    gunitinug is offline Member
    Join Date
    Dec 2016
    Posts
    13
    Rep Power
    0

    Default Re: Wait and NotifyAll

    Java Code:
    public class JoyMethods {
      boolean joy=false;
      
      public synchronized void guardedJoy() {
        while(!joy) {
          try {
            wait();
          } catch(Exception e){}
          System.out.println("Joy has been achieved!");
        }
      }
      
      public synchronized void notifyJoy() {
        joy=true;
        notifyAll();
      }
    }
    
    public class Joy implements Runnable {
      JoyMethods jm;
      
      public Joy(JoyMethods jm) {
        this.jm=jm;
      }
      
      public void run() {
        jm.guardedJoy();
      }
    }
    
    public class Joy2 implements Runnable {
      JoyMethods jm;
      
      public Joy2(JoyMethods jm) {
        this.jm=jm;
      }
      
      public void run() {
        jm.notifyJoy();
      }
    }
    
    public class JoyMain {
      
      public static void main(String[] args) {
        JoyMethods jm=new JoyMethods();
        new Thread(new Joy(jm)).start();
        new Thread(new Joy2(jm)).start();
      }
    }
    This works.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Wait and NotifyAll

    Here is a more simplified example. You don't need a boolean to get wait to work.

    Java Code:
    public class WaitDemo {
    
       public static void main(String[] args) {
          new WaitDemo().start();  
       }
       
       public synchronized void start() {
          new Thread(()->doSomething()).start();
          System.out.println("Wait....");
          try {
             wait();
          } catch (InterruptedException ie) {
          }
          System.out.println("Not waiting anymore!");
       }
          
       
       public void doSomething() {
          System.out.println("Doing something...");
          try {
             Thread.sleep(3000);
          } catch (InterruptedException ie) {}
          synchronized(this) { 
             System.out.println("Wakeup thread.");
             notifyAll();
          }
       }
    
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Why wait(), notify(), notifyAll() declared in Object class?
    By Rituparna in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-24-2011, 12:46 AM
  2. how to "notifyAll()" ?
    By MTN in forum Threads and Synchronization
    Replies: 6
    Last Post: 02-06-2011, 02:27 PM
  3. Problems using wait() and notifyAll() with locks
    By cuffJ in forum Threads and Synchronization
    Replies: 6
    Last Post: 08-18-2010, 03:21 AM
  4. notifyAll
    By simorgh in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-04-2010, 03:08 AM
  5. about wait() and notifyALL
    By denis in forum Threads and Synchronization
    Replies: 13
    Last Post: 04-22-2009, 08:28 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
  •