考虑下面的代码:在SwingWorker内部运行ExecutorService是一种很好的做法吗?
SwingWorker<Void, Void> sworker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(5);
try {
for (int j = 0; j < 5; j++) {
Callable<Object> worker = new MyCallableImpl();
Future<Object> future = executor.submit(worker);
array[j] = future.get();
}
} catch (InterruptedException e) {
// some code here
} catch (ExecutionException e) {
// some code here
}
// some code here
executor.shutdown();
return null;
}
};
sworker.execute();
正如我在标题中说:这是调用内部doInBackground SwingWorker类的()方法的ExecutorService一个好的做法呢?这对我的作品(JDK1.7),GUI不会被阻止,并从执行人池多个线程在后台运行的,但我仍然有一些疑惑...
是的,我知道,但我想在SwingWorker中运行多个线程(Callables)。我该如何做到这一点,而不必在SwingWorker中包装Executor? – DoktorNo 2012-03-28 14:55:53
我只是摆脱'SwingWorker'然后。如果任何的这些任务修改摇摆成分(S),使用'SwingUtilities.invokeLater' – mre 2012-03-28 14:58:13
是的,他们正在修改Swing组件包裹该呼叫(未示出代码,以避免混淆)。我会在此期间尝试您的解决方案。 – DoktorNo 2012-03-28 15:00:31