如何取消SwingWorker的执行?
目前我有两个 SwingWorker 线程在后台工作。如果发生异常,该方法将停止工作,但线程仍在运行。
如果发生异常,如何停止执行并终止 doInBackground() 的线程?
this.cancel(true)
不要销毁/关闭线程。我怎样才能实现这个目标?
@Override
protected Boolean doInBackground() throws Exception {
try {
while (true) {
//some code here
return true;
}
} catch (Exception e) {
this.cancel(true); //<-- this not cancel the thread
return false;
}
}
我在 Netbeans 的调试中看到这些线程。
'AWT-EventQueue-0' em execução
'AWT-Windows' em execução
'SwingWorker-pool-1-thread-1' em execução
'SwingWorker-pool-1-thread-2' em execução
//*em execução = in execution
Currently I have two SwingWorker threads doing job on background. If an exception occurs, the method stop to work, but the thread still runnig.
How I do to stop the execution and kill the thread of the doInBackground()
if an exception occurs?
this.cancel(true)
don't destroy/close the thread. How can I achieve this?
@Override
protected Boolean doInBackground() throws Exception {
try {
while (true) {
//some code here
return true;
}
} catch (Exception e) {
this.cancel(true); //<-- this not cancel the thread
return false;
}
}
I see these threads in the debug of Netbeans.
'AWT-EventQueue-0' em execução
'AWT-Windows' em execução
'SwingWorker-pool-1-thread-1' em execução
'SwingWorker-pool-1-thread-2' em execução
//*em execução = in execution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如jzd所说,有方法 取消(布尔mayInterruptIfRunning);例如
编辑:with cancel(true);你必须(总是)捕获异常
java.util.concurrent.CancellationException
as jzd montioned, there is method cancel(boolean mayInterruptIfRunning); for exapmle
EDIT: with cancel(true); you have to (always) cautgh an exception
java.util.concurrent.CancellationException
默认情况下,
SwingWorker
重用工作线程,因此即使doInBackground()
已返回,仍然可以看到执行您的方法的线程,这是完全正常的。您可以通过查看线程名称来识别这一事实,如 NetBeans 所报告的:
SwingWorker-pool-1-thread-1
,其中 池 由管理SwingWorker
。如果您想要更多控制,还可以将
SwingWorker
实例传递给Executor
。只需检查 SwingWorker 和 Executor javadoc 了解更多 信息。
此外,
SwingWorker.cancel()
不应从doInBackground()
调用,而是从另一个线程(通常是 EDT)调用,例如,当用户单击“取消”时进度对话框中的 按钮。By default,
SwingWorker
reuses worker threads, so it is perfectly normal that, even though,doInBackground()
has returned, to still see the thread that executed your method.You can identify this fact by looking at the thread names, as reported by NetBeans:
SwingWorker-pool-1-thread-1
, where that pool is managed bySwingWorker
.If you want more control, you can also pass the
SwingWorker
instance to anExecutor
.Just check SwingWorker and Executor javadoc for further information.
Besides,
SwingWorker.cancel()
is not supposed to be called fromdoInBackground()
but rather from another thread, generally the EDT, e.g. when user clicks a Cancel button in a progress dialog.有一个
cancel()
方法。您的代码必须注意这一点。如果它在异常后继续运行,则您的代码似乎忽略了不应该的异常。There is a
cancel()
method. Your code would have to pay attention to that. If it keeps running after an exception, it would appear that you code is ignoring an exception that it shouldn't be.供任何经过的人参考:当
SwingWorker
被取消时,done()
在doInBackground()
返回之前被调用。https://bugs.java.com/bugdatabase/view_bug?bug_id=6826514
杰夫
FYI for anyone passing through: when a
SwingWorker
is canceled,done()
is called beforedoInBackground()
returns.https://bugs.java.com/bugdatabase/view_bug?bug_id=6826514
Jeff
您需要经常在 doInBackground() 代码中添加 Thread.sleep(1) 调用。在您的睡眠捕获块中,添加
return
。我也遇到了同样的问题,这就像一个魅力。来源:
https://blogs.oracle.com/swinger/entry/swingworker_stop_that_train
You need to add
Thread.sleep(1)
calls in your doInBackground() code every so often. In your sleep catch blocks, addreturn
. I had the same problem, and this worked like a charm.Source:
https://blogs.oracle.com/swinger/entry/swingworker_stop_that_train
直到 SwingWoker 修复了 https://bugs.java.com/bugdatabase/view_bug?bug_id =6826514
这是一个简单(经过测试)的版本,具有基本(类似)功能,然后是 SwingWoker
Till The SwingWoker it fixed https://bugs.java.com/bugdatabase/view_bug?bug_id=6826514
Here a simple (tested) version with the basic (similar) functions then SwingWoker