首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java: IllegalMonitorStateException on notify()

Java: IllegalMonitorStateException on notify()
EN

Stack Overflow用户
提问于 2013-05-07 00:38:12
回答 1查看 788关注 0票数 0

我尝试在"slp“对象上调用wait()函数,然后在1000个mills之后将其唤醒,而不是消息”已完成...“在调用notify()之后,我得到了一个"IllegalMonitorStateException“错误

代码语言:javascript
复制
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();
 }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-07 00:45:32

您需要是“对象监视器的所有者”才能在it.Your上调用notify方法在this上是同步的,而您在其他对象上是notify()的。给wait()notify()打电话就行了。

IllegalMonitorStateException抛出,指示线程已尝试等待对象的监视器,或通知其他等待对象的监视器但不拥有指定监视器的线程。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16403189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档