等待并通知问题
当我在下面的代码中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
wait
方法需要包含在synchronized
方法或lock
块中,并将对象锁定在您要等待的对象上。在您的情况下,您应该使方法
同步
,这相当于调用lock (this)
。Your
wait
method needs to be enclosed in asynchronized
method or alock
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 callinglock (this)
.您必须通过获取 wait 变量的同步来启动
wait
,例如,请阅读 等待 小心并严格执行,否则你会遇到令人讨厌的意外。
You have to start
wait
by acquiring synchronization on the wait variable, e.g.Please read the javadoc for wait carefully and follow it to the letter, otherwise you'll be up to nasty surprises.
线程使用监视器类包装notify/notifyAll/wait,
线。
runnable、resume - pool runnable)
任何这些方法都会消除您的问题,问题本身是您尝试通知已经通知的线程,其问题与启动已经启动的线程相同。它将抛出 IllegalMonitorStateException。
Thread 是 Process 的一个写得很糟糕的解决方案,但它并不难管理。
and Thread wraps notify/notifyAll/wait with monitor class
thread.
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.