这是从我的程序中获得的。它打开一个带有按钮的窗口。当我单击按钮时,它应该关闭窗口并唤醒其他线程。但是,它永远不会唤醒另一个线程。底部的“返回主题”从不打印,我也不知道为什么。
public class BrandingGui {
public synchronized void replaceImage() throws IOException{
...
JButton keepButton = new JButton("Keep");
keepButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
synchronized(BrandingGui.class) {
//BrandingGui.this.notify();
notifyAll();
}
}
});
...
try {
synchronized(BrandingGui.class) {
this.wait();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("returned to thread");
}
}
答案 0 :(得分:2)
看起来(从注释中)它可以正常工作,所以很好-但是要解决您的OP代码:
在同步时使用类级锁时,您需要在等待/通知时使用类级锁-而不要将实例锁(this)与类级锁混合使用:
public class MyClass {
public static void main(String args[]) {
Thread t = new Thread(new Runnable() {
public void run() {
synchronized (MyClass.class) {
MyClass.class.notifyAll();
System.out.println("Awake");
}
}
});
t.start();
synchronized (MyClass.class) {
try {
System.out.println("here");
MyClass.class.wait();
} catch (InterruptedException ie) {
}
}
System.out.println("Done");
}
}
打印:
here
Awake
Done
在您的OP情况下,您使用的是synchronized
的类级别锁,而使用的是this
的{{1}}对象锁:
notifyall
因此在这种情况下,它必须是:
synchronized(BrandingGui.class) {
notifyAll(); // WRONG - this is the `this` instance lock.
}