如何取消SwingWorker的执行?
|
目前,我有两个SwingWorker线程在后台执行工作。如果发生异常,该方法将停止工作,但线程仍会运行。
如果发生异常,我该如何停止执行并杀死ѭ0的线程?
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
没有找到相关结果
已邀请:
6 个回复
逝媳蘑贩茄
)来识别这一事实,该线程池由
管理。 如果需要更多控制,还可以将
实例传递给
。 只需检查SwingWorker和Executor javadoc以获取更多信息。 此外,不应从``0''调用``10'',而应从另一个线程(通常是EDT)中调用``10''。当用户单击进度对话框中的“取消”按钮时。
呸溉
呕蹿尉
方法。您的代码将必须注意这一点。如果它在出现异常后仍继续运行,则似乎您的代码正在忽略它不应该出现的异常。
镶骄册筷
长拳
。我遇到了同样的问题,这就像一个魅力。 资源: https://blogs.oracle.com/swinger/entry/swingworker_stop_that_train
屡倒雷图