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:com.espertech.esper.example.marketdatafeed.FeedSimMain.java

public void run() {
    if (isWaitKeypress) {
        System.out.println("...press enter to start simulation...");
        try {//  w w w  .j  a  v a  2s.c o  m
            System.in.read();
        } catch (IOException e) {
            log.error("Exception reading keyboard input: " + e.getMessage(), e);
        }
    }

    // Configure engine with event names to make the statements more readable.
    // This could also be done in a configuration file.
    Configuration configuration = new Configuration();
    configuration.addEventType("MarketDataEvent", MarketDataEvent.class.getName());

    // Get engine instance
    EPServiceProvider epService = EPServiceProviderManager.getProvider(engineURI, configuration);

    // Set up statements
    TicksPerSecondStatement tickPerSecStmt = new TicksPerSecondStatement(epService.getEPAdministrator());
    tickPerSecStmt.addListener(new RateReportingListener());

    TicksFalloffStatement falloffStmt = new TicksFalloffStatement(epService.getEPAdministrator());
    falloffStmt.addListener(new RateFalloffAlertListener());

    // For continuous non-ending simulation
    if (continuousSimulation) {
        new MarketDataSendRunnable(epService, true).run();
    } else {
        // Send events
        ExecutorService threadPool = Executors.newFixedThreadPool(numberOfThreads);
        MarketDataSendRunnable runnables[] = new MarketDataSendRunnable[numberOfThreads];
        for (int i = 0; i < numberOfThreads; i++) {
            runnables[i] = new MarketDataSendRunnable(epService, false);
            threadPool.submit(runnables[i]);
        }

        int seconds = 0;
        Random random = new Random();
        while (seconds < numSeconds) {
            seconds++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                log.info("Interrupted", e);
                break;
            }

            FeedEnum feedToDropOff;
            if (random.nextDouble() * 100 < dropProbability) {
                feedToDropOff = FeedEnum.FEED_A;
                if (random.nextBoolean()) {
                    feedToDropOff = FeedEnum.FEED_B;
                }
                log.info("Setting drop-off for feed " + feedToDropOff);

            } else {
                feedToDropOff = null;
            }
            for (int i = 0; i < runnables.length; i++) {
                runnables[i].setRateDropOffFeed(feedToDropOff);
            }
        }

        log.info("Shutting down threadpool");
        for (int i = 0; i < runnables.length; i++) {
            runnables[i].setShutdown();
        }
        threadPool.shutdown();
        try {
            threadPool.awaitTermination(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            // no action
        }
    }
}

From source file:com.opengamma.bbg.replay.BloombergTickWriterTest.java

@Test(invocationCount = 5, successPercentage = 19)
public void performance() throws Exception {
    ExecutorService writerExecutor = Executors.newSingleThreadExecutor();
    Future<?> writerFuture = writerExecutor.submit(_writer);

    double nStartTime = System.currentTimeMillis();

    //create ticks generators
    List<RandomTicksGeneratorJob> ticksGeneratorsList = new ArrayList<RandomTicksGeneratorJob>();
    List<Thread> ticksGeneratorThreads = new ArrayList<Thread>();
    for (int i = 0; i < TICKS_GENERATOR_THREAD_SIZE; i++) {
        RandomTicksGeneratorJob ticksGeneratorJob = new RandomTicksGeneratorJob(
                new ArrayList<String>(_ticker2buid.keySet()), _allTicksQueue);
        ticksGeneratorsList.add(ticksGeneratorJob);
        Thread thread = new Thread(ticksGeneratorJob, "TicksGenerator" + i);
        thread.start();//from   www  . jav a 2 s . c  om
        ticksGeneratorThreads.add(thread);
    }

    s_logger.info("Test running for 1min to gather stats");
    Thread.sleep(RUN_DURATION);

    for (RandomTicksGeneratorJob ticksGeneratorJob : ticksGeneratorsList) {
        ticksGeneratorJob.terminate();
    }

    //wait for all ticksGenerator threads to finish
    for (Thread thread : ticksGeneratorThreads) {
        thread.join();
    }

    //send terminate message for tickWriter to terminate
    sendTerminateMessage();

    //test should fail if writer throws an exception
    writerFuture.get();
    writerExecutor.shutdown();
    writerExecutor.awaitTermination(1, TimeUnit.SECONDS);

    double nRunDuration = System.currentTimeMillis() - nStartTime;

    double nTicks = ((double) _writer.getNTicks() / nRunDuration) * 1000;
    s_logger.info("ticks {}/s", nTicks);
    double nWrites = ((double) _writer.getNWrites() / nRunDuration) * 1000;
    s_logger.info("fileOperations {}/s", nWrites);
    double nBlocks = (double) _writer.getNBlocks() / (double) _writer.getNWrites();
    s_logger.info("average blocks {}bytes", nBlocks);

    assertTrue("reportInterval > testRunTime", REPORT_INTERVAL > nRunDuration);
    if ((nWrites * nBlocks) < WRITER_SPEED_THRESHOLD) {
        s_logger.warn("BloombergTickWriter looks like running really slower than {}b/s",
                WRITER_SPEED_THRESHOLD);
    }
}

From source file:com.gs.collections.impl.parallel.SerialParallelLazyPerformanceTest.java

private void detect(FastList<Integer> collection, int index, boolean expectedResult) {
    MutableList<Runnable> runnables = FastList.newList();
    runnables.add(() -> this.basicSerialDetectPerformance(collection, PREDICATES_LAMBDA.get(index),
            expectedResult, SERIAL_RUN_COUNT));
    int cores = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(cores);
    runnables.add(() -> this.basicParallelLazyDetectPerformance(collection, PREDICATES_LAMBDA.get(index),
            "Lambda", expectedResult, PARALLEL_RUN_COUNT, cores, service));
    runnables.add(() -> this.basicParallelLazyDetectPerformance(collection, PREDICATES.get(index), "Predicate",
            expectedResult, PARALLEL_RUN_COUNT, cores, service));
    runnables.add(() -> this.basicParallelLazyDetectPerformance(collection, PREDICATES_METHOD_REF.get(index),
            "MethodRef", expectedResult, PARALLEL_RUN_COUNT, cores, service));
    this.shuffleAndRun(runnables);
    service.shutdown();
    try {/*ww w . j  a va  2 s.  c o  m*/
        service.awaitTermination(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.siva.javamultithreading.MultiThreadExecutor.java

/**
 * This is sample.//from  w  w  w. ja v a  2s.c om
 */
private static void startProcess() {

    ExecutorService threadPool = Executors.newFixedThreadPool(4);
    CompletionService<HSSFWorkbook> pool = new ExecutorCompletionService<>(threadPool);
    // Excel creation through multiple threads
    long startTime = System.currentTimeMillis();
    pool.submit(new ExcelChunkSheetWriter(0, 1000));
    pool.submit(new ExcelChunkSheetWriter(1001, 20000));
    pool.submit(new ExcelChunkSheetWriter(2, 3000));
    pool.submit(new ExcelChunkSheetWriter(3, 40000));
    pool.submit(new ExcelChunkSheetWriter(4, 50000));

    HSSFWorkbook hSSFWorkbook = null;
    HSSFWorkbook book = new HSSFWorkbook();
    HSSFSheet sheet = book.createSheet("Report");

    try {
        for (int i = 0; i < 5; i++) {
            hSSFWorkbook = pool.take().get();
            System.out.println(
                    "sheet row count : sheet.PhysicalNumberOfRows() = " + sheet.getPhysicalNumberOfRows());
            int currentCount = sheet.getPhysicalNumberOfRows();
            int incomingCount = hSSFWorkbook.getSheetAt(0).getPhysicalNumberOfRows();
            if ((currentCount + incomingCount) > 60000) {
                sheet = book.createSheet("Report" + i);
            }
            ExcelUtil.copySheets(book, sheet, hSSFWorkbook.getSheetAt(0));
        }
    } catch (InterruptedException ex) {
        Logger.getLogger(MultiThreadExecutor.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ExecutionException ex) {
        Logger.getLogger(MultiThreadExecutor.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        writeFile(book, new FileOutputStream("Report.xls"));
    } catch (Exception e) {
        e.printStackTrace();
    }

    /*
     FileOutputStream fos = new FileOutputStream("all.zip");
     ZipOutputStream zos = new ZipOutputStream(fos);
     for (int i = 0; i < 5; i++) {
     try {
     hSSFWorkbook = pool.take().get();                
     ZipEntry ze = new ZipEntry("Excel" + i + ".xls");
     zos.putNextEntry(ze);
     hSSFWorkbook.write(zos);
     zos.closeEntry();
     } catch (InterruptedException ex) {
     Logger.getLogger(MultiThreadExecutor.class.getName()).log(Level.SEVERE, null, ex);
     }           
     }
     zos.close();
     */
    long endTime = System.currentTimeMillis();
    System.out.println("Time taken: " + (endTime - startTime) + " ms");
    threadPool.shutdown();
}

From source file:com.ironiacorp.http.impl.httpclient3.HttpJobRunnerHttpClient3.java

public void run() {
    ExecutorService executor = Executors.newFixedThreadPool(maxThreadsCount);
    ExecutorCompletionService<HttpJob> queue = new ExecutorCompletionService<HttpJob>(executor);
    List<Future<?>> workers = new ArrayList<Future<?>>();

    for (HttpJob job : jobs) {
        if (HttpMethod.GET == job.getMethod()) {
            GetRequest request = new GetRequest(httpClient, job);
            Future<HttpJob> jobStatus = queue.submit(request);
            workers.add(jobStatus);//from w  ww.ja va2 s.com
            continue;
        }
        if (HttpMethod.POST == job.getMethod()) {
            PostRequest request = new PostRequest(httpClient, job);
            Future<HttpJob> jobStatus = queue.submit(request);
            workers.add(jobStatus);
            continue;
        }
        // TODO: job cannot be handled, what to do?
    }

    while (!workers.isEmpty()) {
        Iterator<Future<?>> i = workers.iterator();
        while (i.hasNext()) {
            try {
                Future<?> future = i.next();
                // future.get(timeout, TimeUnit.MILLISECONDS);
                future.get();
                i.remove();
                // } catch (TimeoutException e) {
            } catch (InterruptedException ie) {
                System.out.println(ie.getMessage());
            } catch (ExecutionException ee) {
                System.out.println(ee.getMessage());
                i.remove();
            }
        }
    }

    executor.shutdown();
}

From source file:com.sm.store.client.ClusterClient.java

public List<KeyValue> query(String queryStr) {
    List<KeyCluster> keyClusterList = findAll();
    OpType opType = OpType.QueryStatement;
    int size = keyClusterList.size();
    CountDownLatch countDownLatch = new CountDownLatch(size);
    ExecutorService executor = Executors.newFixedThreadPool(size);
    List<Runnable> runnableList = new ArrayList<Runnable>(size);
    for (int i = 0; i < size; i++) {
        Request request = create4GetPutRequest(keyClusterList.get(i).getKeyList(), opType, queryStr);
        RunThread runThread = new RunThread(request, keyClusterList.get(i), countDownLatch,
                OpType.QueryStatement);/*w  w w.j  av a  2  s  .  co m*/
        runnableList.add(runThread);
        executor.submit(runThread);
    }
    try {
        countDownLatch.await(timeout * 5, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ex) {
        logger.warn(ex.getMessage(), ex);
    } finally {
        executor.shutdown();
        return mergeResponse(runnableList);
    }
}

From source file:com.sm.store.client.ClusterClient.java

private List<KeyValue> executeRequest(List<KeyCluster> keyClusterList, OpType opType, String queryStr,
        List list) {/*from   w  ww  .  java2s .  com*/
    int size = keyClusterList.size();
    //OpType opType = ( queryStr == null ? OpType.MultiGets : OpType.MultiSelectQuery);
    CountDownLatch countDownLatch = new CountDownLatch(size);
    ExecutorService executor = Executors.newFixedThreadPool(size);
    List<Runnable> runnableList = new ArrayList<Runnable>(size);
    for (int i = 0; i < size; i++) {
        Request request = create4GetPutRequest(list, opType, queryStr);
        RunThread runThread = new RunThread(request, keyClusterList.get(i).getCluster(), countDownLatch,
                keyClusterList.get(i).getKeyList().get(0), opType);
        runnableList.add(runThread);
        executor.submit(runThread);
    }
    try {
        countDownLatch.await(timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ex) {
        logger.warn(ex.getMessage(), ex);
    } finally {
        executor.shutdown();
        return mergeResponse(runnableList);
    }
}

From source file:io.undertow.server.handlers.JDBCLogDatabaseTestCase.java

@Test
public void testLogLotsOfThreadsToDatabase()
        throws IOException, InterruptedException, ExecutionException, SQLException {

    JDBCLogHandler logHandler = new JDBCLogHandler(HELLO_HANDLER, DefaultServer.getWorker(), "combined", ds);

    CompletionLatchHandler latchHandler;
    DefaultServer/*from w ww  . j  a v a 2  s. c  om*/
            .setRootHandler(latchHandler = new CompletionLatchHandler(NUM_REQUESTS * NUM_THREADS, logHandler));

    ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    try {
        final List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < NUM_THREADS; ++i) {
            final int threadNo = i;
            futures.add(executor.submit(new Runnable() {
                @Override
                public void run() {
                    TestHttpClient client = new TestHttpClient();
                    try {
                        for (int i = 0; i < NUM_REQUESTS; ++i) {
                            HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
                            HttpResponse result = client.execute(get);
                            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
                            final String response = HttpClientUtils.readResponse(result);
                            Assert.assertEquals("Hello", response);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        client.getConnectionManager().shutdown();
                    }
                }
            }));
        }
        for (Future<?> future : futures) {
            future.get();
        }
    } finally {
        executor.shutdown();
    }

    latchHandler.await();
    logHandler.awaitWrittenForTest();

    Connection conn = null;
    Statement statement = null;
    try {
        conn = ds.getConnection();
        statement = conn.createStatement();

        ResultSet resultDatabase = conn.createStatement().executeQuery("SELECT COUNT(*) FROM PUBLIC.ACCESS;");

        resultDatabase.next();
        Assert.assertEquals(resultDatabase.getInt(1), NUM_REQUESTS * NUM_THREADS);

    } finally {
        if (statement != null) {
            statement.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}

From source file:com.alibaba.cobar.client.CobarSqlMapClientTemplate.java

private ExecutorService createExecutorForSpecificDataSource(CobarDataSourceDescriptor descriptor) {
    final String identity = descriptor.getIdentity();
    final ExecutorService executor = createCustomExecutorService(descriptor.getPoolSize(),
            "createExecutorForSpecificDataSource-" + identity + " data source");
    // 1. register executor for disposing explicitly
    internalExecutorServiceRegistry.add(executor);
    // 2. dispose executor implicitly
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override//from w w w . j a v  a2  s  .c  o m
        public void run() {
            if (executor == null) {
                return;
            }

            try {
                executor.shutdown();
                executor.awaitTermination(5, TimeUnit.MINUTES);
            } catch (InterruptedException e) {
                logger.warn("interrupted when shuting down the query executor:\n{}", e);
            }
        }
    });
    return executor;
}

From source file:com.turn.ttorrent.common.Torrent.java

private static String hashFiles(List<File> files, int pieceLenght)
        throws InterruptedException, IOException, NoSuchAlgorithmException {
    int threads = getHashingThreadsCount();
    ExecutorService executor = Executors.newFixedThreadPool(threads);
    ByteBuffer buffer = ByteBuffer.allocate(pieceLenght);
    List<Future<String>> results = new LinkedList<Future<String>>();
    StringBuilder hashes = new StringBuilder();

    long length = 0L;
    int pieces = 0;

    long start = System.nanoTime();
    for (File file : files) {
        logger.info("Hashing data from {} with {} threads ({} pieces)...", new Object[] { file.getName(),
                threads, (int) (Math.ceil((double) file.length() / pieceLenght)) });

        length += file.length();/*from   w  w  w  . j  a  v  a 2 s  . co m*/

        FileInputStream fis = new FileInputStream(file);
        FileChannel channel = fis.getChannel();
        int step = 10;

        try {
            while (channel.read(buffer) > 0) {
                if (buffer.remaining() == 0) {
                    buffer.clear();
                    results.add(executor.submit(new CallableChunkHasher(buffer)));
                }

                if (results.size() >= threads) {
                    pieces += accumulateHashes(hashes, results);
                }

                if (channel.position() / (double) channel.size() * 100f > step) {
                    logger.info("  ... {}% complete", step);
                    step += 10;
                }
            }
        } finally {
            channel.close();
            fis.close();
        }
    }

    // Hash the last bit, if any
    if (buffer.position() > 0) {
        buffer.limit(buffer.position());
        buffer.position(0);
        results.add(executor.submit(new CallableChunkHasher(buffer)));
    }

    pieces += accumulateHashes(hashes, results);

    // Request orderly executor shutdown and wait for hashing tasks to
    // complete.
    executor.shutdown();
    while (!executor.isTerminated()) {
        Thread.sleep(10);
    }
    long elapsed = System.nanoTime() - start;

    int expectedPieces = (int) (Math.ceil((double) length / pieceLenght));
    logger.info("Hashed {} file(s) ({} bytes) in {} pieces ({} expected) in {}ms.", new Object[] { files.size(),
            length, pieces, expectedPieces, String.format("%.1f", elapsed / 1e6), });

    return hashes.toString();
}