0
//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();
 } 

在其中一个工作人员失败后,从帮助线程中取消一个线程。然后,completionService.take().get() 给出 CancellationException,因为它已经被取消了。

结果,myRunnableObj2 对象没有返回到池中,因为 get() 在取消时抛出异常。这怎么可能处理。?

4

0 回答 0