我想這種線性循環轉換成並行之一:讓它非常:處理數組同時
for(Item item : ItemList) {
processItem(item);
}
這真的是做到這一點的最簡單的辦法?
class Worker implements Runnable {
Item item;
Worker(Item item) {
this.item = item;
}
public void Run() {
processItem(item);
}
}
ExecutorService exec = Executors.newFixedThreadPool(THREADPOOL_SIZE);
for(Item item : ItemList) {
exec.execute(new Worker(item));
}
exec.shutdown();
boolean properFinish = false;
try {
properFinish = exec.awaitTermination(50, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
具體來說,我想辦法使用匿名類,但實際上,沒有辦法讓這個短更易讀,將不勝感激。
UPDATE:剛剛意識到我是一個有點傻,因爲它很容易在這個例子中使用匿名類:
for(final Item item : ItemList) {
exec.execute(new Runnable() {
public void run() {
processItem(item);
}
});
}
在我的原代碼,循環是一個簡單的for (i=0; i<=ItemList.length(); i++)
和我想不到以任何合理的方式讓我成爲最後的一種方式。我想用「爲每個」循環使情況更好。
還有,如何擺脫樣板的其餘部分?
UPDATE 2:使用ExecutorCompletionService
,假定processItem
返回一個結果。
ExecutorService exec = Executors.newFixedThreadPool(THREADPOOL_SIZE);
CompletionService<ResultType> ecs = new ExecutorCompletionService<ResultType>(executor);
for(final Item item : ItemList) {
ecs.submit(new Callable<ResultType>() {
public ResultType call() {
return processItem(item);
}
});
}
for(Item item : item) {
// Do whatever with the results
ecs.take().get();
}
這看起來更好。
謹慎的話,上面的代碼可能會死鎖,如果由於某種原因,你的一個線程不會返回。您可能希望使用輪詢(長時間超時,TimeUnit單位)來代替。 – Gandalf 2009-05-27 15:44:19