Dok*_*rNo 6 java concurrency swing
考虑以下代码:
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();
Run Code Online (Sandbox Code Playgroud)
正如我在标题中所说:在SwingWorker的doInBackground()方法中调用ExecutorService是一个好习惯吗?它适用于我(JDK1.7),GUI没有被阻止,Executor池中的多个线程在后台运行,但我仍然有些疑惑......
上面的代码对我来说没有多大意义。
如果这里的目标是确保 GUI 在执行长时间运行的任务时保持响应,则无需使用,ExecutorService因为SwingWorker已经提供了该机制。
| 归档时间: |
|
| 查看次数: |
1578 次 |
| 最近记录: |