Example usage for com.google.common.util.concurrent ListeningExecutorService shutdownNow

List of usage examples for com.google.common.util.concurrent ListeningExecutorService shutdownNow

Introduction

In this page you can find the example usage for com.google.common.util.concurrent ListeningExecutorService shutdownNow.

Prototype

List<Runnable> shutdownNow();

Source Link

Document

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

Usage

From source file:com.facebook.buck.cli.DistBuildCommandDelegate.java

private void killExecutor(ListeningExecutorService stampedeControllerExecutor, String failureWarning)
        throws InterruptedException {
    stampedeControllerExecutor.shutdown();
    if (!stampedeControllerExecutor.awaitTermination(STAMPEDE_EXECUTOR_SHUTDOWN_TIMEOUT_MILLIS,
            TimeUnit.MILLISECONDS)) {
        LOG.warn(failureWarning);/*from   w w w  .ja v  a 2s  .c  om*/
        stampedeControllerExecutor.shutdownNow();
    }
}

From source file:org.apache.crunch.hadoop.mapreduce.lib.jobcontrol.CrunchJobControl.java

synchronized private void executeReadySeqDoFns() {
    Set<Target> unfinished = getUnfinishedTargets();
    Set<PipelineCallable<?>> oldPipelineCallables = activePipelineCallables;
    this.activePipelineCallables = Sets.newHashSet();
    List<Callable<PipelineCallable.Status>> callablesToRun = Lists.newArrayList();
    for (final PipelineCallable<?> pipelineCallable : oldPipelineCallables) {
        if (Sets.intersection(allPipelineCallables.get(pipelineCallable), unfinished).isEmpty()) {
            if (pipelineCallable.runSingleThreaded()) {
                try {
                    if (pipelineCallable.call() != PipelineCallable.Status.SUCCESS) {
                        failedCallables.add(pipelineCallable);
                    }/*  www .j  a v  a2s  . co m*/
                } catch (Throwable t) {
                    pipelineCallable.setMessage(t.getLocalizedMessage());
                    failedCallables.add(pipelineCallable);
                }
            } else {
                callablesToRun.add(pipelineCallable);
            }
        } else {
            // Still need to run this one
            activePipelineCallables.add(pipelineCallable);
        }
    }

    ListeningExecutorService es = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    try {
        List<Future<PipelineCallable.Status>> res = es.invokeAll(callablesToRun);
        for (int i = 0; i < res.size(); i++) {
            if (res.get(i).get() != PipelineCallable.Status.SUCCESS) {
                failedCallables.add((PipelineCallable) callablesToRun.get(i));
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
        failedCallables.addAll((List) callablesToRun);
    } finally {
        es.shutdownNow();
    }
}

From source file:com.netflix.metacat.main.services.search.ElasticSearchMetacatRefresh.java

private void shutdown(final ListeningExecutorService executorService) {
    if (executorService != null) {
        executorService.shutdown();/*from  w  w  w  .  ja  v a2s  . co m*/
        try {
            // Wait a while for existing tasks to terminate
            if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                executorService.shutdownNow(); // Cancel currently executing tasks
                // Wait a while for tasks to respond to being cancelled
                if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                    log.warn("Thread pool for metacat refresh did not terminate");
                }
            }
        } catch (InterruptedException ie) {
            // (Re-)Cancel if current thread also interrupted
            executorService.shutdownNow();
            // Preserve interrupt status
            Thread.currentThread().interrupt();
        }
    }
}

From source file:com.netflix.metacat.main.services.search.ElasticSearchRefresh.java

private void shutdown(@Nullable final ListeningExecutorService executorService) {
    if (executorService != null) {
        executorService.shutdown();//from  w  w  w .  j av a2s . com
        try {
            // Wait a while for existing tasks to terminate
            if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                executorService.shutdownNow(); // Cancel currently executing tasks
                // Wait a while for tasks to respond to being cancelled
                if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                    log.warn("Thread pool for metacat refresh did not terminate");
                }
            }
        } catch (InterruptedException ie) {
            // (Re-)Cancel if current thread also interrupted
            executorService.shutdownNow();
            // Preserve interrupt status
            Thread.currentThread().interrupt();
        }
    }
}

From source file:org.apache.crunch.impl.spark.SparkRuntime.java

private void runCallables(Set<Target> unfinished) {
    Set<PipelineCallable<?>> oldCallables = activePipelineCallables;
    activePipelineCallables = Sets.newHashSet();
    List<PipelineCallable<?>> callablesToRun = Lists.newArrayList();
    List<PipelineCallable<?>> failedCallables = Lists.newArrayList();
    for (PipelineCallable<?> pipelineCallable : oldCallables) {
        if (Sets.intersection(allPipelineCallables.get(pipelineCallable), unfinished).isEmpty()) {
            if (pipelineCallable.runSingleThreaded()) {
                try {
                    if (pipelineCallable.call() != PipelineCallable.Status.SUCCESS) {
                        failedCallables.add(pipelineCallable);
                    }/* w  ww  . j  a va 2s. com*/
                } catch (Throwable t) {
                    pipelineCallable.setMessage(t.getLocalizedMessage());
                    failedCallables.add(pipelineCallable);
                }
            } else {
                callablesToRun.add(pipelineCallable);
            }
        } else {
            // Still need to run this one
            activePipelineCallables.add(pipelineCallable);
        }
    }

    ListeningExecutorService es = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    try {
        List<Future<PipelineCallable.Status>> res = es.invokeAll(callablesToRun);
        for (int i = 0; i < res.size(); i++) {
            if (res.get(i).get() != PipelineCallable.Status.SUCCESS) {
                failedCallables.add((PipelineCallable) callablesToRun.get(i));
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
        failedCallables.addAll((List) callablesToRun);
    } finally {
        es.shutdownNow();
    }

    if (!failedCallables.isEmpty()) {
        LOG.error("{} callable failure(s) occurred:", failedCallables.size());
        for (PipelineCallable<?> c : failedCallables) {
            LOG.error("{} : {}", c.getName(), c.getMessage());
        }
        status.set(Status.FAILED);
        set(PipelineResult.EMPTY);
        doneSignal.countDown();
    }
}

From source file:com.skcraft.launcher.install.HttpDownloader.java

/**
 * Prevent further downloads from being queued and download queued files.
 *
 * @throws InterruptedException thrown on interruption
 * @throws IOException thrown on I/O error
 *//*from  w  w  w.j ava 2  s.c  o  m*/
public void execute() throws InterruptedException, IOException {
    synchronized (this) {
        queue = Collections.unmodifiableList(queue);
    }

    ListeningExecutorService executor = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(threadCount));

    try {
        List<ListenableFuture<?>> futures = new ArrayList<ListenableFuture<?>>();

        synchronized (this) {
            for (HttpDownloadJob job : queue) {
                futures.add(executor.submit(job));
            }
        }

        try {
            Futures.allAsList(futures).get();
        } catch (ExecutionException e) {
            throw new IOException("Something went wrong", e);
        }

        synchronized (this) {
            if (failed.size() > 0) {
                throw new IOException(failed.size() + " file(s) could not be downloaded");
            }
        }
    } finally {
        executor.shutdownNow();
    }
}

From source file:org.apache.crunch.io.impl.FileTargetImpl.java

@Override
public void handleOutputs(Configuration conf, Path workingPath, int index) throws IOException {
    FileSystem srcFs = workingPath.getFileSystem(conf);
    Path src = getSourcePattern(workingPath, index);
    Path[] srcs = FileUtil.stat2Paths(srcFs.globStatus(src), src);
    FileSystem dstFs = path.getFileSystem(conf);
    if (!dstFs.exists(path)) {
        dstFs.mkdirs(path);/*from   www. j  a va 2 s .  c o  m*/
    }
    boolean sameFs = isCompatible(srcFs, path);
    List<ListenableFuture<Boolean>> renameFutures = Lists.newArrayList();
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(
            Executors.newFixedThreadPool(conf.getInt(RuntimeParameters.FILE_TARGET_MAX_THREADS, 1)));
    for (Path s : srcs) {
        Path d = getDestFile(conf, s, path, s.getName().contains("-m-"));
        renameFutures.add(executorService.submit(new WorkingPathFileMover(conf, s, d, srcFs, dstFs, sameFs)));
    }
    LOG.debug("Renaming " + renameFutures.size() + " files.");

    ListenableFuture<List<Boolean>> future = Futures.successfulAsList(renameFutures);
    List<Boolean> renameResults = null;
    try {
        renameResults = future.get();
    } catch (InterruptedException | ExecutionException e) {
        Throwables.propagate(e);
    } finally {
        executorService.shutdownNow();
    }
    if (renameResults != null && !renameResults.contains(false)) {
        dstFs.create(getSuccessIndicator(), true).close();
        LOG.debug("Renamed " + renameFutures.size() + " files.");
    }
}

From source file:org.apache.hive.ptest.execution.Phase.java

protected List<RemoteCommandResult> initalizeHosts() throws Exception {
    List<ListenableFuture<List<RemoteCommandResult>>> futures = Lists.newArrayList();
    ListeningExecutorService executor = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(hostExecutors.size()));
    try {/*w w  w .  ja va2 s  . c o  m*/
        for (final HostExecutor hostExecutor : hostExecutors) {
            futures.add(executor.submit(new Callable<List<RemoteCommandResult>>() {
                @Override
                public List<RemoteCommandResult> call() throws Exception {
                    return initalizeHost(hostExecutor);
                }
            }));
        }
        List<RemoteCommandResult> results = Lists.newArrayList();
        for (ListenableFuture<List<RemoteCommandResult>> future : futures) {
            List<RemoteCommandResult> result = future.get();
            if (result != null) {
                results.addAll(result);
            }
        }
        executor.shutdown();
        return results;
    } finally {
        if (executor.isShutdown()) {
            executor.shutdownNow();
        }
    }
}

From source file:com.facebook.buck.cli.AdbCommandRunner.java

/**
 * Execute an {@link AdbCallable} for all matching devices. This functions performs device
 * filtering based on three possible arguments:
 *
 *  -e (emulator-only) - only emulators are passing the filter
 *  -d (device-only) - only real devices are passing the filter
 *  -s (serial) - only device/emulator with specific serial number are passing the filter
 *
 *  If more than one device matches the filter this function will fail unless multi-install
 *  mode is enabled (-x). This flag is used as a marker that user understands that multiple
 *  devices will be used to install the apk if needed.
 *///w  w w  .  j  a  v a 2s  . c o m
@VisibleForTesting
protected boolean adbCall(AdbOptions options, TargetDeviceOptions deviceOptions, ExecutionContext context,
        AdbCallable adbCallable) {

    // Initialize adb connection.
    AndroidDebugBridge adb = createAdb(context);
    if (adb == null) {
        console.printBuildFailure("Failed to create adb connection.");
        return false;
    }

    // Build list of matching devices.
    List<IDevice> devices = filterDevices(adb.getDevices(), options, deviceOptions);
    if (devices == null) {
        return false;
    }

    int adbThreadCount = options.getAdbThreadCount();
    if (adbThreadCount <= 0) {
        adbThreadCount = devices.size();
    }

    // Start executions on all matching devices.
    List<ListenableFuture<Boolean>> futures = Lists.newArrayList();
    ListeningExecutorService executorService = listeningDecorator(
            newMultiThreadExecutor(getClass().getSimpleName(), adbThreadCount));
    for (final IDevice device : devices) {
        futures.add(executorService.submit(adbCallable.forDevice(device)));
    }

    // Wait for all executions to complete or fail.
    List<Boolean> results = null;
    try {
        results = Futures.allAsList(futures).get();
    } catch (ExecutionException ex) {
        console.printBuildFailure("Failed: " + adbCallable);
        ex.printStackTrace(console.getStdErr());
        return false;
    } catch (InterruptedException ex) {
        console.printBuildFailure("Interrupted.");
        ex.printStackTrace(console.getStdErr());
        return false;
    } finally {
        executorService.shutdownNow();
    }

    int successCount = 0;
    for (Boolean result : results) {
        if (result) {
            successCount++;
        }
    }
    int failureCount = results.size() - successCount;

    // Report results.
    if (successCount > 0) {
        console.printSuccess(String.format("Succesfully ran %s on %d device(s)", adbCallable, successCount));
    }
    if (failureCount > 0) {
        console.printBuildFailure(String.format("Failed to %s on %d device(s).", adbCallable, failureCount));
    }

    return failureCount == 0;
}

From source file:org.sosy_lab.cpachecker.core.algorithm.ParallelAlgorithm.java

@Override
public AlgorithmStatus run(ReachedSet pReachedSet) throws CPAException, InterruptedException {
    mainEntryNode = AbstractStates.extractLocation(pReachedSet.getFirstState());
    ForwardingReachedSet forwardingReachedSet = (ForwardingReachedSet) pReachedSet;

    ListeningExecutorService exec = listeningDecorator(newFixedThreadPool(configFiles.size()));
    List<ListenableFuture<ParallelAnalysisResult>> futures = new ArrayList<>();

    for (AnnotatedValue<Path> p : configFiles) {
        futures.add(exec.submit(createParallelAnalysis(p, ++stats.noOfAlgorithmsUsed)));
    }//from  ww  w.j ava  2s. c o m

    // shutdown the executor service,
    exec.shutdown();

    handleFutureResults(futures);

    // wait some time so that all threads are shut down and have (hopefully) finished their logging
    if (!exec.awaitTermination(10, TimeUnit.SECONDS)) {
        logger.log(Level.WARNING, "Not all threads are terminated although we have a result.");
    }

    exec.shutdownNow();

    if (finalResult != null) {
        forwardingReachedSet.setDelegate(finalResult.getReached());
        return finalResult.getStatus();
    }

    return AlgorithmStatus.UNSOUND_AND_PRECISE;
}