我有一个关于基于等待/通知的线程交互的问题。
下面代码的输出是Im
. Im
由于没有其他线程调用notify()
Thread 对象,因此输出如何。是否像 JVMnotify()
在您尝试等待 Thread 类实例的上述场景中隐式调用。
线程操作在等待而没有收到任何通知时会卡住。现在,如果我等待 Thread 类实例怎么办wait()
。例如
public class WaitingThread {
public static void main(String[] args) {
Thread t1 = new Thread();
t1.start();
synchronized (t1) {
try {
t1.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Im");
}
}