[英]how to notify waiting main thread from user thread in java?
class A implements Runnable
{
public void run()
{
try
{
Thread.sleep(1000);
}
catch(Exception e){};
notify();//This has to wake up the sleeping main thread.
}
public static void main(String args[])
{
A a=new A();
Thread t=new Thread(a);
t.start();
try
{
Thread.wait(5000);
}
catch(Exception e){};
System.out.println("Rest of the code");
}
}
当我编译此代码时,JVM表示无法从静态主方法中引用非静态方法等待。
当我将其放在其他方法中时,它将显示非法的监视器异常。
请为我提出解决此问题的方法。 不要建议加入。 因为我必须等待并且单独通知。
几件事情:
ClassName.wait
请参阅此以获取有关wait api的详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.