Example usage for java.util.concurrent ExecutorService shutdown

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

Introduction

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

Prototype

void shutdown();

Source Link

Document

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

Usage

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 }) {
        {//from   w w w  .  j ava  2s  .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-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: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 }) {
        {/* w w  w.  jav a 2 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"));
}

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

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

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

    for (int size : new int[] { 64, 128, 256 }) {
        {//from ww w .j  a va 2s. 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-vanilla-chronicle"));
            }

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

            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-vanilla-chronicle"));
}

From source file:com.brienwheeler.lib.concurrent.ExecutorsTest.java

@Test
public void testNewSingleThreadExecutorShutdownClean() throws InterruptedException {
    NamedThreadFactory threadFactory = new NamedThreadFactory(THREAD_FACTORY_NAME);
    ExecutorService executor = Executors.newSingleThreadExecutor(threadFactory);
    Assert.assertFalse(executor.isShutdown());
    Assert.assertFalse(executor.isTerminated());

    executor.execute(new NullRunnable());

    executor.shutdown();
    Assert.assertTrue(executor.isShutdown());
    executor.awaitTermination(10, TimeUnit.MILLISECONDS);
    Assert.assertTrue(executor.isTerminated());
}

From source file:idc.storyalbum.fetcher.FilterService.java

public Set<Photo> filter(Set<Photo> photos, Set<String> tags)
        throws ExecutionException, InterruptedException, FlickrException {
    log.info("Removing from {} photos photos without tags {}", photos.size(), tags);
    ExecutorService executorService = Executors.newFixedThreadPool(20);
    List<Future<Pair<Photo, Photo>>> futures = new ArrayList<>();
    for (Photo photo : photos) {
        futures.add(executorService.submit(new FilterTagTask(photo, tags)));
    }//from   www  .j  a va 2  s. c  o m
    executorService.shutdown();
    Set<Photo> result = new HashSet<>();
    for (Future<Pair<Photo, Photo>> future : futures) {
        Pair<Photo, Photo> photoBooleanPair = future.get();
        Photo photo = photoBooleanPair.getLeft();
        String url;
        try {
            url = photo.getOriginalUrl();
        } catch (Exception e) {
            url = photo.getUrl();
        }
        Photo detailedPhoto = photoBooleanPair.getRight();
        if (detailedPhoto == null) {
            log.info("Filtered {}", url);
            photos.remove(photo);
        } else {
            result.add(detailedPhoto);
        }
    }
    return result;

}

From source file:jenkins.plugins.elanceodesk.workplace.notifier.HttpWorkerTest.java

@Test
public void testMutipleTriesWorker() throws InterruptedException {
    ExecutorService executorService = Executors.newCachedThreadPool();
    HttpWorker worker = new HttpWorker("http://localhost:8000/retry-test", "test1body", 30000, retries,
            Mockito.mock(PrintStream.class));
    executorService.submit(worker);//from   www  .ja v  a 2  s. com
    executorService.shutdown();
    executorService.awaitTermination(5, TimeUnit.SECONDS);
    Assert.assertTrue(MyHandler.getRetryTestResult());
}

From source file:com.espertech.esper.multithread.TestMTStmtNamedWindowMerge.java

private void trySend(int numThreads, int numEventsPerThread) throws Exception {
    Configuration config = SupportConfigFactory.getConfiguration();
    config.addEventType("SupportBean", SupportBean.class);
    engine = EPServiceProviderManager.getDefaultProvider(config);
    engine.initialize();/*from ww w  . j  a v  a  2  s .c  om*/

    // setup statements
    engine.getEPAdministrator().createEPL("create window MyWindow.win:keepall() as select * from SupportBean");
    engine.getEPAdministrator()
            .createEPL("on SupportBean sb " + "merge MyWindow nw where nw.theString = sb.theString "
                    + " when not matched then insert select * "
                    + " when matched then update set intPrimitive = nw.intPrimitive + 1");

    // execute
    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    Future<Boolean> future[] = new Future[numThreads];
    for (int i = 0; i < numThreads; i++) {
        future[i] = threadPool.submit(new StmtNamedWindowMergeCallable(engine, numEventsPerThread));
    }

    threadPool.shutdown();
    threadPool.awaitTermination(10, TimeUnit.SECONDS);

    // total up result
    for (int i = 0; i < numThreads; i++) {
        Boolean result = future[i].get();
        assertTrue(result);
    }

    // compare
    EventBean[] rows = engine.getEPRuntime().executeQuery("select * from MyWindow").getArray();
    assertEquals(numEventsPerThread, rows.length);
    for (EventBean row : rows) {
        assertEquals(numThreads - 1, row.get("intPrimitive"));
    }
    //long deltaTime = endTime - startTime;
    //System.out.println("Totals updated: " + totalUpdates + "  Delta cumu: " + deltaCumulative + "  Delta pooled: " + deltaTime);
}

From source file:com.espertech.esper.multithread.TestMTDeterminismListener.java

private void trySend(int numThreads, int numEvents, boolean isPreserveOrder,
        ConfigurationEngineDefaults.Threading.Locking locking) throws Exception {
    Configuration config = SupportConfigFactory.getConfiguration();
    config.getEngineDefaults().getThreading().setListenerDispatchPreserveOrder(isPreserveOrder);
    config.getEngineDefaults().getThreading().setListenerDispatchLocking(locking);

    engine = EPServiceProviderManager.getDefaultProvider(config);
    engine.initialize();/*from  w w w . java2s  .  com*/

    // setup statements
    EPStatement stmtInsert = engine.getEPAdministrator()
            .createEPL("select count(*) as cnt from " + SupportBean.class.getName());
    SupportMTUpdateListener listener = new SupportMTUpdateListener();
    stmtInsert.addListener(listener);

    // execute
    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    Future future[] = new Future[numThreads];
    for (int i = 0; i < numThreads; i++) {
        future[i] = threadPool.submit(new SendEventCallable(i, engine, new GeneratorIterator(numEvents)));
    }

    threadPool.shutdown();
    threadPool.awaitTermination(10, TimeUnit.SECONDS);

    for (int i = 0; i < numThreads; i++) {
        assertTrue((Boolean) future[i].get());
    }

    EventBean events[] = listener.getNewDataListFlattened();
    long[] result = new long[events.length];
    for (int i = 0; i < events.length; i++) {
        result[i] = (Long) events[i].get("cnt");
    }
    //log.info(".trySend result=" + Arrays.toString(result));

    // assert result
    assertEquals(numEvents * numThreads, events.length);
    for (int i = 0; i < numEvents * numThreads; i++) {
        assertEquals(result[i], (long) i + 1);
    }
}

From source file:com.lumata.lib.lupa.extractor.internal.HtmlBiggestImageExtractor.java

@Override
public Image extractBestImage(URL sourceUrl, Elements htmlSection, ImageExtractionRequirements requirements) {
    Map<String, Image> imagesToExplore = new HashMap<String, Image>();
    Set<ImageDownloadTask> imagesToDownload = new HashSet<ImageDownloadTask>();
    Iterator<org.jsoup.nodes.Element> it = htmlSection.iterator();

    // collect valid images
    while (it.hasNext() && imagesToExplore.size() < requirements.getMaxImagesToExplore()) {
        Element imageElement = it.next();
        String imageUrl = imageElement.absUrl("src");

        // Do not process empty img tags, duplicated images or tracking
        // pixels and other assorted ads
        if (imageUrl == null || imagesToExplore.containsKey(imageUrl) || isTrackingPixelOrAd(imageUrl)) {
            continue;
        }//from   www  .  j  a v  a 2s  .  c om

        // remember this image
        Image imageContent = new Image(imageUrl);
        if (imageElement.hasAttr(WIDTH_ATTRIBUTE)) {
            // TODO: We need to convert other picture size units supported by html (there must be a lib for this)
            imageContent.setWidth(Integer.parseInt(imageElement.attr(WIDTH_ATTRIBUTE).replace("px", "")));
        }
        if (imageElement.hasAttr(HEIGHT_ATTRIBUTE)) {
            imageContent.setHeight(Integer.parseInt(imageElement.attr(HEIGHT_ATTRIBUTE).replace("px", "")));
        }
        if (imageContent.getWidth() == null || imageContent.getHeight() == null) {// mark image to download
            imagesToDownload.add(new ImageDownloadTask(imageContent));
        }
        imagesToExplore.put(imageUrl, imageContent);
    }

    // if dimensions are empty -> download image
    if (CollectionUtils.isNotEmpty(imagesToDownload)) {
        try {
            ExecutorService pool = Executors.newFixedThreadPool(imagesToDownload.size(),
                    getThreadFactory(sourceUrl));
            pool.invokeAll(imagesToDownload);
            pool.shutdown();
        } catch (InterruptedException e) {
            LOG.error("InterruptedException while downloading images", e);
        }
    }

    // select biggest image
    Image biggestImage = null;
    try {
        biggestImage = Collections.max(imagesToExplore.values(), new Comparator<Image>() {
            @Override
            public int compare(Image o1, Image o2) {
                return getSquarePixels(o1) - getSquarePixels(o2);
            }
        });
    } catch (NoSuchElementException e) {
        return null;
    }

    // if image is too small, discard
    return (biggestImage.getWidth() < requirements.getMinImageSize()
            || biggestImage.getHeight() < requirements.getMinImageSize()) ? null : biggestImage;
}

From source file:com.navercorp.pinpoint.collector.receiver.tcp.TCPReceiver.java

private void shutdownExecutor(ExecutorService executor) {
    if (executor == null) {
        return;/*  w  w  w .j a v  a  2s. co  m*/
    }
    executor.shutdown();
    try {
        executor.awaitTermination(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}