【发布时间】:2012-04-12 04:31:01
【问题描述】:
考虑以下代码:
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();
正如我在标题中所说:这是在 SwingWorker 的 doInBackground() 方法中调用 ExecutorService 的好习惯吗?它适用于我(JDK1.7),GUI 没有被阻塞,并且来自 Executor 池的多个线程在后台运行,但我仍然有一些疑问......
【问题讨论】:
标签: java swing concurrency