有一個循環輪詢從GenericObjectPool中借用對象。池本身的大小爲1.代碼如下 -將對象返回到單獨的線程池中
final CompletionService completionService = new ExecutorCompletionService(getExecutorServices());
int counter = 0;
for (Iterator iter = AList.iterator(); iter.hasNext();) {
borrowed = this.getPool().borrowObject();
if (borrowed == null) {
throw new Exception("not set");
} else {
completionService.submit(borrowed,borrowed);
counter ++;
}
}
由於池的大小爲1,所以在第一次借入後,它會被耗盡並被阻塞。 要返回的對象回到游泳池,我想運行下面一個單獨的線程 -
new Runnable() {
public void run() {
for (int i = 0; i < counter; i++) {
borrowed = completionService.take().get();
status = borrowed.getStatus();
getPool().returnObject(borrowed);
counter --;
if (status = 1) {
getExecutorServices().shutdownNow();
return;
}
}
}
};
這是一個阻塞調用CompletionService作用於每個線程完成並釋放它使其可用於借。
但是這種設計有一些缺點,例如不能從Runnable中讀取父代的計數器。
在第一個代碼塊中,借來的對象被重複提交。那應該沒問題? – fortm 2013-02-21 13:08:58
completionService.submit(借用,借用)來自另一個問題的另一個答案,我相信。您需要這樣做是因爲您提交了一個可運行的程序,並希望具有相同類型的可運行結果,因此您可以稍後重新提交。 CS通常用於執行返回結果的可調用對象,但您也可以將它用於可運行對象。就像在這個例子中。 – 2013-02-21 13:18:35