我有myThread.wait()
那个是synchronzed(myThread)
块。我有实现可运行的 Myrunner。我想告诉notify()
myRunner,但它不是监控对象。是否可以从 myRunnable 获取 myThread 的句柄以进行通知?还有其他解决方案吗?myRunnable
由于某些与我的代码相关的原因,从扩展Thread
并运行它并不好。
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);
}
}
}
}