我尝试在"slp“对象上调用wait()函数,然后在1000个mills之后将其唤醒,而不是消息”已完成...“在调用notify()之后,我得到了一个"IllegalMonitorStateException“错误
class Class1 extends Thread{
boolean newTxt = false;
private Sleep slp = null;
synchronized public void put(Sleep slp)
{
thus.slp = slp;
try{ slp.wait();}catch(Exception x){}
}
synchronized public void wakeup()
{
slp.notify();
}
public void run()
{
while(slp == null );
try{ sleep(1000);}catch(Exception x){}
wakeup();
}
}
class Sleep extends Thread {
Class1 t;
Sleep(Class1 t) {
this.t=t;
}
public void run() {
System.out.println("Started");
t.put(this);
System.out.println("Finished after 1000 mills");
}
}
public class Koord {
public static void main(String[] args) {
Class1 t = new Class1();
Sleep t1 = new Sleep(t);
t1.start();
t.start();
}
}发布于 2013-05-07 00:45:32
您需要是“对象监视器的所有者”才能在it.Your上调用notify方法在this上是同步的,而您在其他对象上是notify()的。给wait()和notify()打电话就行了。
IllegalMonitorStateException抛出,指示线程已尝试等待对象的监视器,或通知其他等待对象的监视器但不拥有指定监视器的线程。
https://stackoverflow.com/questions/16403189
复制相似问题