java - Notify() from runnable -


i have mythread.wait() in synchronzed(mythread) block. , have myrunner implements runnable. tell notify() myrunner, not monitor object. possible handle of mythread myrunnable make notify? there other solution? extend myrunnable thread , run not reasons related on code specific.

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

both threads should synchronize using same object. also, should not use existing object syncronize, create object used explicitly synchronization, like

object lock = new object(); 

also see https://www.securecoding.cert.org/confluence/display/java/lck01-j.+do+not+synchronize+on+objects+that+may+be+reused

if lock used interact thread, can put in thread , provide getter use it.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -