我正在尝试做什么:
启动窗口以请求参数
我尝试了什么:
vp.wait()
,则窗口会消失。notify()
,程序就不会等了。这是我的代码:
public static void main(String[] args) throws InterruptedException{
if(args.length==0){
ParamsWind vp = new ParamsWind();
vp.setVisible(true);
synchronized (vp){
try {
vp.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
.....
public class ParamsWind extends JDialog {
...
public ParamsWind()
....
//Create Ok Button and program Action Listener
Button ok = new Button("OK");
...
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(f.getText().equals("") || r.getText().equals("")){
ErrorWind verr = new ErrorWind();
verr.setVisible(true);
}
notify();
答案 0 :(得分:0)
这就是actionPerformed
中的问题。
notify();
你没有synchronized
阻止这样做,所以线程不拥有this
的监视器...因此异常。
但是,您只是 想要synchronized
阻止,因为您实际上是在错误的对象上调用notify()
。您想使用ParamsWind
:
synchronized(ParamsWind.this) {
ParamsWind.this.notify();
}
我不清楚使用wait()
和notify()
真的是你想要的 - 或者你最终不会遇到竞争条件 - 但那些是立即与您正在做的事情有关的问题。