[英]Java thread wait() notify() in one method
public class Signal2NoiseRatio
{
public ImagePlus SingleSNR(ImagePlus imagePlus) throws InterruptedException
{
new Thread()
{
@Override public void run()
{
for (int i = 0; i <= 1; i++)
{
System.out.println("in queue"+i);
}
synchronized( this )
{
this.notify();
}
}
}.start();
synchronized (this)
{
this.wait();
}
return imagePlusToProcess;
}
}
notify()
does not reach wait()
. notify()
没有达到wait()
。
what's wrong here? 这有什么不对?
It is essential for me that both synchonized methods in this one method are implemented. 对我来说,必须实现这一方法中的两种同步方法。
the main thread perform a frame which presents an image in it. 主线程执行一个框架,在其中呈现图像。 Is it possible that the
wait()
method leads the frame to a white window? wait()
方法是否可能将框架引导到白色窗口?
this
in the SingleSNR
method and this
in the overridden run
method are not the same object (inside run
, this
refers to the anonymous subclass of Thread
). this
在SingleSNR
方法和this
在重写run
方法是不一样的对象(内run
, this
指的是匿名子类Thread
)。 You need to make sure you are notifying the same object you wait
for, which is available as Signal2NoiseRatio.this
: 您需要确保通知您
wait
的同一对象,可以使用Signal2NoiseRatio.this
:
@Override public void run()
{
for (int i = 0; i <= 1; i++)
{
System.out.println("in queue"+i);
}
synchronized( Signal2NoiseRatio.this )
{
Signal2NoiseRatio.this.notify();
}
}
两个“这个”是不一样的,一个是Signal2NoiseRatio,一个是Thread
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.