【发布时间】:2016-11-01 17:32:18
【问题描述】:
我一直在阅读这方面的内容,从这些帖子中,我知道我可能是一个错误
SwingWorker, done() is executed before process() calls are finished
The proper way to handle exceptions thrown by the SwingWorker.doInBackground
但是在我的例子中,没有任何东西可以调用 done 方法。这就是我的工人的工作方式。
public static class collectingWorker extends SwingWorker<Void, Void> {
collectingWorker(String name) {
//initialize
threadName = name;
}
@Override
public Void doInBackground() {
loop = true;
while(loop){
//Code goes here
}
return null;
}
@Override
protected void done() {
System.out.println("loop: " + loop);
System.out.println("Collecting worker DONE");
try {
get();
} catch (CancellationException x) {
System.out.println("Cancelation");
// ...
} catch (InterruptedException x) {
System.out.println("Interruption");
// ...
} catch (ExecutionException x) {
System.out.println("Execution");
System.out.println(x.getMessage());
System.out.println(x.getCause());
// ...
}
}
}
在一个单独的线程上,我有一个计数器,它在将循环设置为 false 之前等待 x 分钟。我看到的是 collectWorker done 方法在它应该等待的 x 分钟之前执行,更糟糕的是,它是完全随机的,有时它可以工作,有时它会在 3 分钟后失败,有时会在 90 分钟后失败。
这就是我从 done 方法的打印中得到的,正如你所看到的,布尔“循环”永远不会设置为 false
loop: true
Collecting worker DONE
Execution
java.util.NoSuchElementException
java.util.NoSuchElementException
有什么想法或解决方法吗?
【问题讨论】:
标签: java multithreading swingworker