java - notify() not working in Runnable -


why can't notification reader in code:

public class threadmain {     public thread reader;     private class serialreader implements runnable          {         public void run()              {                 try                 {                 thread.sleep(3000);                  synchronized(this)                     {                     system.out.println("notifying");                     notify();                     }                 thread.sleep(3000);                 } catch (exception e){}             }         }      threadmain()     {     reader = (new thread (new serialreader())   );     }      public static void main(string [] args) {     threadmain d= new threadmain();         d.reader.start();         synchronized(d)         {             try             {                 d.wait();             system.out.println("got notify");             } catch (exception e) { system.out.println(e); }          }      } } 

i have line notifying in output

the notify , wait not use same monitor there no chance "talk" each other.

one simple fix use reader monitor in main:

synchronized (d.reader) {     try {         d.reader.wait();         system.out.println("got notify");     } catch (exception e) {         system.out.println(e);     } } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -