在所有`FutureTask`完成计算后,如何调用另一个方法?

时间:2013-02-20 12:24:23

标签: listener futuretask

我有以下方法在不同的PaaS中创建和部署应用程序:

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的内容,但我不确定这些是正确的还是在这种情况下如何实现它们。

1 个答案:

答案 0 :(得分:0)

您确实需要ListenableFuture,请在此页面上搜索关键字“粉丝”。

使用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
}