Example usage for java.util.concurrent ExecutorService awaitTermination

List of usage examples for java.util.concurrent ExecutorService awaitTermination

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService awaitTermination.

Prototype

boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;

Source Link

Document

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Usage

From source file:org.apache.streams.elasticsearch.ElasticsearchPersistReader.java

protected void shutdownAndAwaitTermination(ExecutorService pool) {
    pool.shutdown(); // Disable new tasks from being submitted
    try {/*from   w w w.j  ava 2s  .  c o  m*/
        // Wait a while for existing tasks to terminate
        if (!pool.awaitTermination(10, TimeUnit.SECONDS)) {
            pool.shutdownNow(); // Cancel currently executing tasks
            // Wait a while for tasks to respond to being cancelled
            if (!pool.awaitTermination(10, TimeUnit.SECONDS))
                LOGGER.error("Pool did not terminate");
        }
    } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted
        pool.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
    }
}

From source file:org.apache.hadoop.hbase.trace.IntegrationTestSendTraceRequests.java

@Test
public void internalDoWork() throws Exception {
    util = createUtil();/*  www  . j  a  va  2s.com*/
    admin = util.getHBaseAdmin();
    setupReceiver();

    deleteTable();
    createTable();
    LinkedBlockingQueue<Long> rks = insertData();

    ExecutorService service = Executors.newFixedThreadPool(20);
    doScans(service, rks);
    doGets(service, rks);

    service.shutdown();
    service.awaitTermination(100, TimeUnit.SECONDS);
    Thread.sleep(90000);
    receiverHost.closeReceivers();
    util.restoreCluster();
    util = null;
}

From source file:com.mgmtp.jfunk.core.JFunk.java

private void shutDownExecutorService(final ExecutorService execService) {
    try {/*from   w  ww. jav a  2 s.  c o  m*/
        execService.shutdownNow();
        execService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    } catch (InterruptedException ex) {
        LOG.error("Script execution was interrupted.", ex);
    }
}

From source file:net.openhft.chronicle.logger.slf4j.Slf4jIndexedChronicleBinaryLoggerPerfTest.java

@Test
public void testMultiThreadLogging() throws IOException, InterruptedException {
    warmup(LoggerFactory.getLogger("perf-binary-indexed-chronicle"));

    final int RUNS = 1000000;
    final int THREADS = 10;

    for (int size : new int[] { 64, 128, 256 }) {
        final long start = System.nanoTime();

        ExecutorService es = Executors.newFixedThreadPool(THREADS);
        for (int t = 0; t < THREADS; t++) {
            es.submit(new RunnableLogger(RUNS, size, "perf-binary-indexed-chronicle"));
        }/*from w ww. j  av  a2  s . c o  m*/

        es.shutdown();
        es.awaitTermination(30, TimeUnit.SECONDS);

        final long time = System.nanoTime() - start;

        System.out.printf(
                "ChronicleLog.MT (runs=%d, min size=%03d, elapsed=%.3f ms) took an average of %.3f us per entry\n",
                RUNS, size, time / 1e6, time / 1e3 / (RUNS * THREADS));
    }

    ChronicleTools.deleteOnExit(basePath("perf-binary-indexed-chronicle"));
}

From source file:cn.vko.cache.dao.ha.FailoverHotSwapDataSourceCreator.java

private void shutdownExecutor(ExecutorService executor) {
    try {/* w  w w.j a  va2s  .  c  o  m*/
        executor.shutdown();
        executor.awaitTermination(5, TimeUnit.SECONDS);
    } catch (Exception ex) {
        logger.warn("interrupted when shutting down executor service.");
    }
}

From source file:com.janrain.backplane2.server.config.Backplane2Config.java

@PreDestroy
private void cleanup() {
    Metrics.shutdown();//from  w  ww  .  j a  va  2  s .c om

    for (ExecutorService executor : backgroundServices) {
        try {
            executor.shutdown();
            if (executor.awaitTermination(10, TimeUnit.SECONDS)) {
                logger.info("Background thread shutdown properly");
            } else {
                executor.shutdownNow();
                if (!executor.awaitTermination(10, TimeUnit.SECONDS)) {
                    logger.error("Background thread did not terminate");
                }
            }
        } catch (InterruptedException e) {
            logger.error("error shutting down background service", e);
            executor.shutdownNow();
            Thread.currentThread().interrupt();
        }
    }
}

From source file:net.openhft.chronicle.logger.slf4j.Slf4jVanillaChronicleLoggerPerfTest.java

@Test
public void testMultiThreadLogging() throws IOException, InterruptedException {
    warmup(LoggerFactory.getLogger("perf-vanilla-chronicle"));

    final int RUNS = 300000;
    final int THREADS = Runtime.getRuntime().availableProcessors();

    for (int size : new int[] { 64, 128, 256 }) {
        {/*  www .j av a2 s . c om*/
            final long start = System.nanoTime();

            ExecutorService es = Executors.newFixedThreadPool(THREADS);
            for (int t = 0; t < THREADS; t++) {
                es.submit(new RunnableLogger(RUNS, size, "perf-vanilla-chronicle"));
            }

            es.shutdown();
            es.awaitTermination(2, TimeUnit.MINUTES);

            final long time = System.nanoTime() - start;

            System.out.printf(
                    "Plain.MT (runs=%d, min size=%03d, elapsed=%.3f ms) took an average of %.3f us per entry\n",
                    RUNS, size, time / 1e6, time / 1e3 / (RUNS * THREADS));
        }
    }

    ChronicleTools.deleteOnExit(basePath("perf-vanilla-chronicle"));
}

From source file:org.deegree.tools.coverage.converter.RasterConverter.java

private void shutdownExecutorAndWaitForFinish(ExecutorService executor) throws InterruptedException {
    executor.shutdown();//w  w w .j a va2 s . c om
    /** oh them tonny */
    executor.awaitTermination(42, TimeUnit.DAYS);

}

From source file:org.apache.drill.exec.service.ServiceEngine.java

@Override
public void close() throws Exception {
    // this takes time so close them in parallel
    // Ideally though we fix this netty bug: https://github.com/netty/netty/issues/2545
    ExecutorService p = Executors.newFixedThreadPool(2);
    submit(p, "userServer", userServer);
    submit(p, "dataPool", dataPool);
    submit(p, "controller", controller);
    p.shutdown();/*w  w w  . j a  v a2  s.  com*/
    try {
        p.awaitTermination(3, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    AutoCloseables.close(userAllocator, controlAllocator, dataAllocator);

}

From source file:net.openhft.chronicle.logger.slf4j.Slf4jIndexedChronicleLoggerPerfTest.java

@Test
public void testMultiThreadLogging() throws IOException, InterruptedException {
    warmup(LoggerFactory.getLogger("perf-binary-indexed-chronicle"));

    final int RUNS = 1000000;
    final int THREADS = 10;

    for (int size : new int[] { 64, 128, 256 }) {
        {// www .  ja  v  a2  s.  c o  m
            final long start = System.nanoTime();

            ExecutorService es = Executors.newFixedThreadPool(THREADS);
            for (int t = 0; t < THREADS; t++) {
                es.submit(new RunnableLogger(RUNS, size, "perf-binary-indexed-chronicle"));
            }

            es.shutdown();
            es.awaitTermination(5, TimeUnit.SECONDS);

            final long time = System.nanoTime() - start;

            System.out.printf(
                    "ChronicleLog.MT (runs=%d, min size=%03d, elapsed=%.3f ms) took an average of %.3f us per entry\n",
                    RUNS, size, time / 1e6, time / 1e3 / (RUNS * THREADS));
        }
    }

    ChronicleTools.deleteOnExit(basePath("perf-binary-indexed-chronicle"));
}