【发布时间】:2022-01-09 16:15:40
【问题描述】:
我是使用线程的新手,一旦调用 wait 方法,我就会收到 IllegalMonitorStateException 有人可以帮助我吗
public class SomeClass{
private final Thread thread = new Thread(this::someMethod);
public synchronized void someMethod(){
try {
doSomething();
TimeUnit.SECONDS.sleep(2);
doSomething();
thread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
someMethod();
}
public synchronized void restartThread() {
thread.notify();
}
SomeClass test = new SomeClass();
test.start();
【问题讨论】:
标签: java multithreading exception