【发布时间】:2015-01-24 16:46:45
【问题描述】:
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 说不能从静态主方法引用非静态方法等待。
当我把它放在不同的方法中时,它会显示非法监视器异常。
请给我一个解决这个问题的方法。不要建议加入之类的东西。因为我必须单独等待和通知。
【问题讨论】:
-
听说过缩进吗?您的代码可能会使用一些...按原样,它的可读性不是很高。
-
我浪费了半个小时尝试使用通知和等待。如果我不能使用连接,我会编写自己的代码来等待线程。这样,我知道它会正确等待。
标签: java multithreading synchronization wait