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(); if lock used interact thread, can put in thread , provide getter use it.
Comments
Post a Comment