//Worker Thread
class MyRunnable implements Runnable {
private String status;
public void getStatus() {
return status;
}
public void run() {
try {
if(Thread.interrupted()) throw new InterruptedException();
}catch(InterruptedException ie) {
status= "FAIL";
return;
}
}
//Thread that submits Workers
MyRunnable myRunnableObj1 = getObjectPool().borrow();
futures.add(completionService.submit(myRunnableObj1,myRunnableObj1));
//A Helper thread to return Objects to pool
new Callable() {
public String call() {
MyRunnable myRunnableObj2 = completionService.take().get();
getObjPool().returnObject(myRunnableObj2):
if(myRunnableObj2.getStatus= "FAIL") {
for(Future future : futures) {
if(!future.isDone())
future.cancel(true);
}
return myRunnableObj2.getStatus();
}
ワーカーの 1 つが失敗した後、ヘルパー スレッドからスレッドがキャンセルされると。その後、既にキャンセルされているため、 completionService.take().get() は CancellationException を返します。
その結果、myRunnableObj2 オブジェクトはプールに返されません。これは、キャンセル時に get() が例外をスローするためです。これはどのように処理できますか。?