重定向到新 URL 时小程序崩溃
我正在开发一个小程序,它可以完成一些工作,然后在用户单击按钮时重定向到 URL。我正在使用 SwingWorker 来防止 GUI 被锁定。
但是当我运行小程序时,单击按钮后出现此错误:
线程“SwingWorker-pool-1-thread-2”中出现异常 java.lang.IllegalMonitorStateException 在 java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(来源未知) 在 java.util.concurrent.locks.AbstractQueuedSynchronizer.release(来源未知) 在 java.util.concurrent.locks.ReentrantLock.unlock(来源未知) 在 java.util.concurrent.LinkedBlockingQueue.poll(来源未知) 在 java.util.concurrent.ThreadPoolExecutor.getTask(来源未知) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(来源未知) 在 java.lang.Thread.run(来源不明)
看起来小程序正在自我销毁,但工作线程仍然存在。如果我添加 Thread.sleep(2000); 作为测试,小程序运行正常,但我认为这不是修复它的正确方法。
我做错了什么??也许我应该从主线程进行重定向?也许小程序的销毁方法应该等待工作线程完成?
Java 源:
public class MyApplet extends javax.swing.JApplet {
class MyWorker extends SwingWorker<Boolean , Void> {
protected Boolean doInBackground() throws Exception {
// DO THE HARD JOB...
}
public void done() {
netscape.javascript.JSObject.getWindow(this).call("onDone", new String[] { "" });
}
}
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
setBackground(Color.WHITE);
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jMyButtonctionPerformed(java.awt.event.ActionEvent evt) {
new MyWorker().execute();
}
}
HTML 源:
<script type="text/javascript">
function onDone(){document.location.href="http://myurl.com";}
</script>
I'm developing an applet that makes some work and then a redirection to an URL when user clicks on a button. I'm using a SwingWorker to prevent GUI gets locked up.
But when I run the applet I get this error after clicking the button:
Exception in thread "SwingWorker-pool-1-thread-2" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(Unknown Source)
at java.util.concurrent.locks.ReentrantLock.unlock(Unknown Source)
at java.util.concurrent.LinkedBlockingQueue.poll(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
It looks like the applet is destroying itself but the worker thread is still alive..If I add a Thread.sleep(2000); as a test, the applet runs right, but I think it's not the right way to fix it.
What I'm doing wrong?? Maybe I should make the redirection from the main thread? Maybe destroy method of the applet should wait to worker thread to finish?
Java source:
public class MyApplet extends javax.swing.JApplet {
class MyWorker extends SwingWorker<Boolean , Void> {
protected Boolean doInBackground() throws Exception {
// DO THE HARD JOB...
}
public void done() {
netscape.javascript.JSObject.getWindow(this).call("onDone", new String[] { "" });
}
}
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
setBackground(Color.WHITE);
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jMyButtonctionPerformed(java.awt.event.ActionEvent evt) {
new MyWorker().execute();
}
}
HTML source:
<script type="text/javascript">
function onDone(){document.location.href="http://myurl.com";}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终使用 SwingUtilities.invokeLater 解决了:D
Finally solved using SwingUtilities.invokeLater :D