Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:notify()上的IllegalMonitorStateException_Java_Multithreading_Notify - Fatal编程技术网

Java:notify()上的IllegalMonitorStateException

Java:notify()上的IllegalMonitorStateException,java,multithreading,notify,Java,Multithreading,Notify,我试图对“slp”对象调用wait()函数,然后在1000 mills唤醒它之后,在调用notify()之后,我得到的不是消息“Finished…”,而是“IllegalMonitorStateException”错误 您需要是“对象监视器的所有者”,才能在其上调用notify。您的方法在此上与其他对象上的notify()同步。只需调用wait()和notify() ,抛出以指示线程试图在对象监视器上等待,或通知在对象监视器上等待的其他线程而不拥有指定的监视器。您需要是“对象监视器的所有者”能够

我试图对“slp”对象调用wait()函数,然后在1000 mills唤醒它之后,在调用notify()之后,我得到的不是消息“Finished…”,而是“IllegalMonitorStateException”错误

您需要是“对象监视器的所有者”,才能在其上调用
notify
。您的方法在此
上与其他对象上的
notify()
同步。只需调用
wait()
notify()

,抛出以指示线程试图在对象监视器上等待,或通知在对象监视器上等待的其他线程而不拥有指定的监视器。

您需要是“对象监视器的所有者”能够对其调用
notify
。您的方法在
this
上与其他对象上的
notify()
同步。只需调用
wait()
notify()


,抛出以指示线程试图在对象监视器上等待,或通知在对象监视器上等待的其他线程而不拥有指定的监视器。

工作,但它会立即显示消息,而不是在1000 millsWorks之后,但会立即显示消息,而不是在1000 millsOk之后,那么我会改变什么呢?这是100个问题的副本。好的,那么我会改变什么呢?这是100个问题的副本。
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();
 }
}