2013-02-20 17 views
0

我有一個創建並在不同的PaaS部署應用以下方法:如何在所有`FutureTask`完成其計算後調用另一個方法?

private void deployModulesInPaaS() { 

     ExecutorService executor = Executors.newFixedThreadPool(listModules 
       .size()); 

     ModuleParsed mod; 

     for (Iterator<ModuleParsed> iterator = listModules.iterator(); iterator 
       .hasNext();) { 
      mod = (ModuleParsed) iterator.next(); 
      try { 
       switch (mod.getId_paas()) { 
       case 1:      
        GAEDeployer gaeDeployer = new GAEDeployer(mod.getId_paas(), 
          mod.getId_component(), "//whatever/path"); 
        FutureTask<URI> gaeFuture = new FutureTask<URI>(gaeDeployer); 
        executor.execute(gaeFuture); 
        mod.setDeployedURI(gaeFuture.get()); 
        break; 
       case 2: 
        AzureDeployer azureDeployer = new AzureDeployer(
          "subscription", "path_certificate", "password", 
          "storageAccountName", "storageAccountKey"); 
        FutureTask<URI> azureFuture = new FutureTask<URI>(
          azureDeployer); 
        executor.execute(azureFuture); 
        mod.setDeployedURI(azureFuture.get()); 
        break;     
       default: 
        System.out.println("The PaaS identifier of module " 
          + mod.getId_component() + " is unknown."); 
        break; 
       } 

      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } catch (ExecutionException e) { 
       e.printStackTrace(); 
      } 
     } 

    } 

我怎麼能再次呼籲所有FutureTask已經完成了他們的計算的另一種方法? 我已閱讀約Command模式和約Listener,但我不確定這些是否是正確的,也不知道如何在這種情況下實現它們。

回答

0

您確實需要ListenableFuture,請在此頁面搜索關鍵字「fan-in」。

CountDownLatch和壓倒一切的FutureTask.done()的另一種方式,不建議:

private void deployModulesInPaaS() { 

    CountDownLatch countDownLatch = new CountDownLatch(listModules.size()); 

    ExecutorService executor = Executors.newFixedThreadPool(listModules 
      .size()); 

    ModuleParsed mod; 

    for (Iterator<ModuleParsed> iterator = listModules.iterator(); iterator 
      .hasNext();) { 
     mod = (ModuleParsed) iterator.next(); 
     try { 
      switch (mod.getId_paas()) { 
      case 1: 
       GAEDeployer gaeDeployer = new GAEDeployer(mod.getId_paas(), 
         mod.getId_component(), "//whatever/path"); 
       FutureTask<URI> gaeFuture = new FutureTask<URI>(gaeDeployer) { 
        @Override 
        protected void done() { 

         super.done(); 
         countDownLatch.countDown(); 
        } 
       }; 
       executor.execute(gaeFuture); 
       mod.setDeployedURI(gaeFuture.get()); 
       break; 
      case 2: 
       AzureDeployer azureDeployer = new AzureDeployer(
         "subscription", "path_certificate", "password", 
         "storageAccountName", "storageAccountKey"); 
       FutureTask<URI> azureFuture = new FutureTask<URI>(
         azureDeployer) { 
        @Override 
        protected void done() { 

         super.done(); 
         countDownLatch.countDown(); 
        } 
       }; 
       executor.execute(azureFuture); 
       mod.setDeployedURI(azureFuture.get()); 
       break; 
      default: 
       System.out.println("The PaaS identifier of module " 
         + mod.getId_component() + " is unknown."); 
       break; 
      } 

     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      e.printStackTrace(); 
     } 
    } 
    countDownLatch.await(); 
    // do finally 
} 
相關問題