简体   繁体   English

线程“ AWT-EventQueue-0”中的异常java.lang.Object.notify上的java.lang.IllegalMonitorStateException(本机方法)

[英]Exception in thread “AWT-EventQueue-0” java.lang.IllegalMonitorStateException at java.lang.Object.notify(Native Method)

What I'm trying to do: 我正在尝试做的是:

Launch a window to request parameters 启动一个窗口以请求参数

What I've tried: 我尝试过的

  1. If I remove the vp.wait() , the window disappears. 如果删除vp.wait() ,则窗口消失。
  2. If I remove notify() , the program doesn`t wait. 如果我删除notify() ,则程序不会等待。

Here's my code: 这是我的代码:

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();

Well here's the problem, in actionPerformed . 好吧,这就是actionPerformed的问题。

notify();

You're doing that without a synchronized block, so the thread doesn't own the monitor for this ... hence the exception. 您是在没有synchronized块的情况下执行此操作的,因此线程不this拥有监视器...因此是例外。

However, you don't just want a synchronized block, because you're actually calling notify() on the wrong object. 但是,您不只是想要一个synchronized块,因为您实际上是在错误的对象上调用notify() You want to use the ParamsWind : 您想使用ParamsWind

synchronized(ParamsWind.this) {
    ParamsWind.this.notify();
}

It's not clear to me that using wait() and notify() is really what you want here - or that you won't end up with a race condition - but those are the immediate problems with what you're doing. 对我来说,尚不清楚使用wait()notify()确实是您想要的-否则您将不会遇到竞争状况-但这是您正在做的直接问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在线程“ AWT-EventQueue-0”中不断提出错误Exception java.lang.IllegalMonitorStateException - Keep coming up with an error Exception in thread “AWT-EventQueue-0” java.lang.IllegalMonitorStateException 如何解决线程“ TimerQueue”中的异常之类的错误?Java中线程“ AWT-EventQueue-0”中的异常?java.lang.IllegalMonitorStateException? - How to solve error like Exception in thread “TimerQueue” Exception in thread “AWT-EventQueue-0” java.lang.IllegalMonitorStateException in java? 通知异常 java.lang.IllegalMonitorStateException Locks - Notify exception java.lang.IllegalMonitorStateException Locks 线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException Java - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Java 线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException? Java的 - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException? Java 线程“ AWT-EventQueue-0”中的异常java.lang.NoClassDefFoundError:java.lang.Class.forName0上的org / joda / time / ReadableInstant(本机方法) - Exception in thread “AWT-EventQueue-0” java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant at java.lang.Class.forName0(Native Method) Java:线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Java: Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Java-线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Java - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException 线程“ AWT-EventQueue-0”中的Java异常java.lang.ArrayIndexOutOfBoundsException:1 - Java Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: 1 线程“ AWT-EventQueue-0”中的java异常java.lang.ClassCastException - java Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM