multithreading - How to use wait() and notify() in Java? -


as understand, suppose call wait() on mutex, when want current thread stop working until thread calls notify() on same mutex object. doesn't seem working.

i'm trying make thread print 1-10. wait thread print 11-20. , first thread again print 21-30

main.java

public class main {     public static void main(string[] args) throws interruptedexception {         object mutex = 1;          thread child1 = new thread(new child1(mutex));         thread child2 = new thread(new child2(mutex));          child1.start();         child2.start();      }  } 

child1.java

public class child1 implements runnable {     object mutex;      public child1(object mutex){         this.mutex = mutex;     }      public void run() {         synchronized (mutex) {             for(int c = 0; c < 10; c++){                 system.out.println(c+1);             }              try {                 wait();             } catch (interruptedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }         }           for(int c = 20; c < 31; c++){             system.out.println(c+1);         }     } } 

child2.java

public class child2 implements runnable {     object mutex;      public child2(object mutex) {         this.mutex = mutex;     }      public void run() {         synchronized (mutex) {             (int c = 11; c < 21; c++) {                 system.out.println(c);             }             notify();         }      } } 

output

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17exception in thread "thread-0"  18 19 20 exception in thread "thread-1" java.lang.illegalmonitorstateexception     @ java.lang.object.wait(native method)     @ java.lang.object.wait(object.java:502)     @ task42.child1.run(child1.java:18)     @ java.lang.thread.run(thread.java:745) java.lang.illegalmonitorstateexception     @ java.lang.object.notify(native method)     @ task42.child2.run(child2.java:15)     @ java.lang.thread.run(thread.java:745) 

what missing?

you must add mutex reference wait() , notify(); is, change wait() mutex.wait() , notify() mutex.notify().

without this, calling wait/notify on this (method() equivalent this.method())

here code appropriate changes made:

child1.java

public class child1 implements runnable {     object mutex;      public child1(object mutex){         this.mutex = mutex;     }      public void run() {         synchronized (mutex) {             for(int c = 0; c < 10; c++){                 system.out.println(c+1);             }              try {                 mutex.wait(); // changed here             } catch (interruptedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }         }           for(int c = 20; c < 31; c++){             system.out.println(c+1);         }     } } 

child2.java

public class child2 implements runnable {     object mutex;      public child2(object mutex) {         this.mutex = mutex;     }      public void run() {         synchronized (mutex) {             (int c = 11; c < 21; c++) {                 system.out.println(c);             }             mutex.notify(); // changed here         }      } } 

Comments