等待并通知问题

发布于 2024-09-27 03:02:23 字数 1418 浏览 0 评论 0原文

当我在下面的代码中使用 wait() 方法时,它会抛出以下异常

Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException

代码如下:

private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                            
        newFileChooser = new JFileChooser();

        int returnVal = newFileChooser.showSaveDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            filename = newFileChooser.getSelectedFile();
            JFrame mainFrame = NetSimApp.getApplication().getMainFrame();
            networktype = new NetType(mainFrame);
            networktype.setLocationRelativeTo(mainFrame);
            NetSimApp.getApplication().show(networktype);
            try {
                this.wait();
            } catch (InterruptedException ex) {
               Logger.getLogger(NetSimView.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (!NetType.validip) {
                statusTextArea.append("File not created:Select Network Type.\n");
            }
            newNodeMenuItem.setEnabled(true);
        } else {
             newNodeMenuItem.setEnabled(false);
            statusTextArea.append("File not created:Access cancelled by user.\n");
        }
    }

实际上我正在调用 jDialog 类的对象,并且我希望对话框对象应该完成首先,然后它应该通知上面给定的代码。我已经在该类中指定了notify()。谁能告诉我问题是什么及其解决方案。 -提前致谢

When I am using the wait() method in the following code its throwing the following Exeption

Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException

The code is as follows:

private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                            
        newFileChooser = new JFileChooser();

        int returnVal = newFileChooser.showSaveDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            filename = newFileChooser.getSelectedFile();
            JFrame mainFrame = NetSimApp.getApplication().getMainFrame();
            networktype = new NetType(mainFrame);
            networktype.setLocationRelativeTo(mainFrame);
            NetSimApp.getApplication().show(networktype);
            try {
                this.wait();
            } catch (InterruptedException ex) {
               Logger.getLogger(NetSimView.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (!NetType.validip) {
                statusTextArea.append("File not created:Select Network Type.\n");
            }
            newNodeMenuItem.setEnabled(true);
        } else {
             newNodeMenuItem.setEnabled(false);
            statusTextArea.append("File not created:Access cancelled by user.\n");
        }
    }

Actually I am calling the object of a jDialog class and i want that the dialog object should complete first and then it should notify the above given code. I've already specified notify() in that class. Can anyone tell me whats the problem and its solution. -Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瑾夏年华 2024-10-04 03:02:23

您的 wait 方法需要包含在 synchronized 方法或 lock 块中,并将对象锁定在您要等待的对象上。

在您的情况下,您应该使方法同步,这相当于调用lock (this)

Your wait method needs to be enclosed in a synchronized method or a lock block, with the object being locked on the object you want to wait upon.

In your case, you should make the method synchronized, which is equivalent to calling lock (this).

追我者格杀勿论 2024-10-04 03:02:23

您必须通过获取 wait 变量的同步来启动 wait,例如,

synchronized( this )
{
    this.wait( );
}

请阅读 等待 小心并严格执行,否则你会遇到令人讨厌的意外。

You have to start wait by acquiring synchronization on the wait variable, e.g.

synchronized( this )
{
    this.wait( );
}

Please read the javadoc for wait carefully and follow it to the letter, otherwise you'll be up to nasty surprises.

沦落红尘 2024-10-04 03:02:23
  1. 同步代码块,其中“wait()”不是
  2. “通知”线程,“通知”可运行,线程是可运行的控制类
    线程使用监视器类包装notify/notifyAll/wait,
  3. 尝试使用中断并抛出一个“非错误”抛出来暂停/恢复
    线。
  4. 使用生产者/消费者方法
  5. 使用 fork/join 方法
  6. 使用线程池来暂停和恢复线程(暂停 - 终止
    runnable、resume - pool runnable)

任何这些方法都会消除您的问题,问题本身是您尝试通知已经通知的线程,其问题与启动已经启动的线程相同。它将抛出 IllegalMonitorStateException。

Thread 是 Process 的一个写得很糟糕的解决方案,但它并不难管理。

  1. synchronized block of code where "wait()" is
  2. don't "notify" Thread, "notify" runnable, Thread is a Control class for runnable
    and Thread wraps notify/notifyAll/wait with monitor class
  3. try using interupt and throw a "non-error" throwable to pause/resume
    thread.
  4. use producent/consumer approach
  5. use fork/join approach
  6. use Thread pool for pausing and resuming thread (pause - kill
    runnable, resume - pool runnable)

Any of these approaches would eliminate your problem, the problem itself is that you try to notify already notified thread, its same problem like starting already started thread. It will throw a IllegalMonitorStateException.

Thread is a horribly written solution to Process but its not so hard to manage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文