我有一个notify()和wait()问题。
我希望主线程能够强制其他线程等待并在需要时通知。
似乎我不了解所有内容,因为通知无法正常工作。我无法访问我的NOTIFY方法。
这是我的代码:
主要思路:
public class troll {
static Runnable R0;
static Thread TH;
public static void main(String[] args) {
System.out.println("TROLOLOLL0");
watek R0 = new watek();
Thread TH = new Thread(R0);
synchronized(TH){
TH.start();
R0.NOTIFY();
}
}
}
我的螺纹类:
public class watek implements Runnable {
public watek(){
}
public void run() {
System.out.print("STOP");
this.WAIT();
System.out.print("Running again");
}
public synchronized void WAIT(){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void NOTIFY(){
notify();
}
}
最佳答案
你只是一场比赛。 NOTIFY
方法中对main
的调用可能在新线程中的wait
之前进行。这样一来,便没有什么可以唤醒它了。