Example usage for java.util.concurrent ExecutorService shutdownNow

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

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService 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.emc.vipr.sync.CasMigrationTest.java

protected void delete(FPPool pool, List<String> clipIds) throws Exception {
    ExecutorService service = Executors.newFixedThreadPool(CAS_SETUP_THREADS);

    System.out.print("Deleting clips");

    for (String clipId : clipIds) {
        service.submit(new ClipDeleter(pool, clipId));
    }/*  w ww.  j a  v a2  s . com*/

    service.shutdown();
    service.awaitTermination(CAS_SETUP_WAIT_MINUTES, TimeUnit.MINUTES);
    service.shutdownNow();

    System.out.println();
}

From source file:com.emc.ecs.sync.CasMigrationTest.java

protected String summarize(FPPool pool, List<String> clipIds) throws Exception {
    List<String> summaries = Collections.synchronizedList(new ArrayList<String>());

    ExecutorService service = Executors.newFixedThreadPool(CAS_THREADS);

    System.out.print("Summarizing clips");

    for (String clipId : clipIds) {
        service.submit(new ClipReader(pool, clipId, summaries));
    }//from ww  w . j av a2  s.  c o m

    service.shutdown();
    service.awaitTermination(CAS_SETUP_WAIT_MINUTES, TimeUnit.MINUTES);
    service.shutdownNow();

    System.out.println();

    Collections.sort(summaries);
    StringBuilder out = new StringBuilder();
    for (String summary : summaries) {
        out.append(summary);
    }
    return out.toString();
}

From source file:com.emc.vipr.sync.CasMigrationTest.java

protected String summarize(FPPool pool, List<String> clipIds) throws Exception {
    List<String> summaries = Collections.synchronizedList(new ArrayList<String>());

    ExecutorService service = Executors.newFixedThreadPool(CAS_SETUP_THREADS);

    System.out.print("Summarizing clips");

    for (String clipId : clipIds) {
        service.submit(new ClipReader(pool, clipId, summaries));
    }/*from   w  w  w. ja  v  a  2  s .  c  om*/

    service.shutdown();
    service.awaitTermination(CAS_SETUP_WAIT_MINUTES, TimeUnit.MINUTES);
    service.shutdownNow();

    System.out.println();

    Collections.sort(summaries);
    StringBuilder out = new StringBuilder();
    for (String summary : summaries) {
        out.append(summary);
    }
    return out.toString();
}

From source file:org.apache.gobblin.ingestion.google.webmaster.GoogleWebmasterDataFetcherImpl.java

/**
 * Get all pages in an async mode./* w  w  w .j a va2s  . co m*/
 */
private Collection<String> getPages(String startDate, String endDate, List<Dimension> dimensions,
        ApiDimensionFilter countryFilter, Queue<Pair<String, FilterOperator>> toProcess, int rowLimit)
        throws IOException {
    String country = GoogleWebmasterFilter.countryFilterToString(countryFilter);

    ConcurrentLinkedDeque<String> allPages = new ConcurrentLinkedDeque<>();
    int r = 0;
    while (r <= GET_PAGES_RETRIES) {
        ++r;
        log.info(String.format("Get pages at round %d with size %d.", r, toProcess.size()));
        ConcurrentLinkedDeque<Pair<String, FilterOperator>> nextRound = new ConcurrentLinkedDeque<>();
        ExecutorService es = Executors.newFixedThreadPool(10, ExecutorsUtils
                .newDaemonThreadFactory(Optional.of(log), Optional.of(this.getClass().getSimpleName())));

        while (!toProcess.isEmpty()) {
            submitJob(toProcess.poll(), countryFilter, startDate, endDate, dimensions, es, allPages, nextRound,
                    rowLimit);
        }
        //wait for jobs to finish and start next round if necessary.
        try {
            es.shutdown();
            boolean terminated = es.awaitTermination(5, TimeUnit.MINUTES);
            if (!terminated) {
                es.shutdownNow();
                log.warn(String.format(
                        "Timed out while getting all pages for country-%s at round %d. Next round now has size %d.",
                        country, r, nextRound.size()));
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        if (nextRound.isEmpty()) {
            break;
        }
        toProcess = nextRound;
    }
    if (r == GET_PAGES_RETRIES) {
        throw new RuntimeException(String.format(
                "Getting all pages reaches the maximum number of retires %d. Date range: %s ~ %s. Country: %s.",
                GET_PAGES_RETRIES, startDate, endDate, country));
    }
    return allPages;
}

From source file:org.springframework.test.context.junit4.SpringMethodRoadie.java

/**
 * Runs the test method on the test instance with the specified
 * <code>timeout</code>.//from  w w  w . j a va 2s .c  om
 *
 * @param timeout the timeout in milliseconds.
 * @see #runWithRepetitions(Runnable)
 * @see #runTestMethod()
 */
protected void runWithTimeout(final long timeout) {
    runWithRepetitions(new Runnable() {

        public void run() {
            final ExecutorService service = Executors.newSingleThreadExecutor();
            final Callable<Object> callable = new Callable<Object>() {

                public Object call() throws Exception {

                    runTestMethod();
                    return null;
                }
            };
            final Future<Object> result = service.submit(callable);
            service.shutdown();
            try {
                boolean terminated = service.awaitTermination(timeout, TimeUnit.MILLISECONDS);
                if (!terminated) {
                    service.shutdownNow();
                }
                // throws the exception if one occurred during the
                // invocation
                result.get(0, TimeUnit.MILLISECONDS);
            } catch (final TimeoutException e) {
                addFailure(new Exception(String.format("test timed out after %d milliseconds", timeout)));
            } catch (final Exception e) {
                addFailure(e);
            }
        }
    });
}

From source file:org.tallison.cc.WReGetter.java

private void execute(String[] args) throws IOException {
    if (args.length != 3) {
        usage();//from w  w  w  .j a  v  a 2  s  .  co m
        System.exit(1);
    }
    if (args[0].contains("-h")) {
        usage();
        System.exit(0);
    }
    int numThreads = Integer.parseInt(args[0]);
    BufferedReader r = Files.newBufferedReader(Paths.get(args[1]));
    ArrayBlockingQueue<DigestURLPair> queue = new ArrayBlockingQueue<DigestURLPair>(1000);
    QueueFiller filler = new QueueFiller(r, queue, numThreads);
    new Thread(filler).start();
    rootDir = Paths.get(args[2]);
    System.out.println("creating thread pool");
    ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
    ExecutorCompletionService<Integer> executorCompletionService = new ExecutorCompletionService<Integer>(
            executorService);
    System.out.println("about to start");

    for (int i = 0; i < numThreads; i++) {
        System.out.println("submitted " + i);
        executorCompletionService.submit(new WGetter(queue));
    }

    int completed = 0;
    while (completed < numThreads) {
        try {
            Future<Integer> future = executorCompletionService.poll(1, TimeUnit.SECONDS);
            if (future != null) {
                completed++;
            }
        } catch (InterruptedException e) {

        }
    }
    executorService.shutdown();
    executorService.shutdownNow();
    System.exit(0);

}

From source file:fr.inria.wimmics.coresetimer.CoreseTimer.java

public CoreseTimer run() throws ClassNotFoundException, IllegalAccessException, InstantiationException,
        IOException, LoadException {
    LOGGER.entering(CoreseTimer.class.getName(), "run");
    assert (initialized);

    // Loading the nq data in corese, then applying several times the query.
    LOGGER.log(Level.INFO, "beginning with input #{0}", test.getInput());
    // require to have a brand new adapter for each new input set.
    adapter = (CoreseAdapter) Class.forName(adapterName).newInstance();

    String inputFileName = "";
    switch (mode) {
    case MEMORY: {
        inputFileName += test.getInput();
        adapter.preProcessing(inputFileName, true);
        break;/*from  w ww. jav  a  2 s  . c o  m*/
    }
    case DB: {
        inputFileName += test.getInputDb();
        System.setProperty("fr.inria.corese.tinkerpop.dbinput", inputFileName);
        adapter.preProcessing(inputFileName, false);
        break;
    }
    }

    String query = test.getRequest();
    LOGGER.log(Level.INFO, "processing nbQuery #{0}", query);
    stats = new DescriptiveStatistics();
    statsMemory = new DescriptiveStatistics();
    int nbCycles = test.getMeasuredCycles() + test.getWarmupCycles();
    boolean measured = true;
    for (int i = 0; i < nbCycles; i++) {
        LOGGER.log(Level.INFO, "iteration #{0}", i);
        System.gc();
        final long startTime = System.currentTimeMillis();
        LOGGER.log(Level.INFO, "before query");

        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<?> future = executor.submit(new Runnable() {
            @Override
            public void run() {
                adapter.execQuery(query);
            }
        });

        try {
            future.get(1, TimeUnit.HOURS);
            measured = true;
        } catch (InterruptedException | TimeoutException e) {
            future.cancel(true);
            measured = false;
            LOGGER.log(Level.WARNING, "Terminated!");
        } catch (ExecutionException ex) {
            Logger.getLogger(CoreseTimer.class.getName()).log(Level.SEVERE, null, ex);
        }
        executor.shutdownNow();

        LOGGER.log(Level.INFO, "after query");
        final long endTime = System.currentTimeMillis();
        long delta = endTime - startTime;
        long memoryUsage = getMemoryUsage();
        LOGGER.info(String.format("elapsed time = %d ms", delta));
        LOGGER.info(String.format("used memory = %d bytes", memoryUsage));
        if (i >= test.getWarmupCycles()) {
            if (!measured) {
                while (i < nbCycles) {
                    stats.addValue(-100);
                    statsMemory.addValue(memoryUsage);
                    i++;
                }
            } else {
                stats.addValue(delta);
                statsMemory.addValue(memoryUsage);
            }
        }
    }
    adapter.saveResults(test.getOutputPath());
    mappings = adapter.getMappings();
    adapter.postProcessing();
    LOGGER.exiting(CoreseTimer.class.getName(), "run");
    return this;
}

From source file:com.teradata.benchto.driver.jdbc.ConnectionPoolTest.java

private void openGivenConnectionsAmountSimultaneously(String dataSourceName, int connectionsCount)
        throws SQLException, InterruptedException, TimeoutException {
    ExecutorService executorService = newFixedThreadPool(connectionsCount);
    ExecutorCompletionService<?> completionService = new ExecutorCompletionService(executorService);
    CountDownLatch countDownLatch = new CountDownLatch(connectionsCount);
    DataSource dataSource = applicationContext.getBean(dataSourceName, DataSource.class);

    range(0, connectionsCount).mapToObj(i -> createQueryRunnable(dataSource, countDownLatch))
            .forEach(completionService::submit);

    try {/*  ww w.  jav a  2 s.  com*/
        for (int i = 0; i < connectionsCount; i++) {
            try {
                Future<?> future = completionService.take();
                future.get(1, MINUTES);
            } catch (ExecutionException e) {
                rethrowException(e.getCause());
            }
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } finally {
        executorService.shutdownNow();
        executorService.awaitTermination(1, MINUTES);
    }
}

From source file:com.legstar.host.server.PoolingTest.java

/**
 * Schedule some basic work./*  w  w  w.  ja  va2 s  . c  o m*/
 * @throws Exception if failure
 */
public void testScheduleWork() throws Exception {
    ExecutorService executor = Executors.newFixedThreadPool(CLIENT_THREADS);
    WorkManager wm = new WorkManagerImpl(executor);
    EngineHandler engHandler = new EngineHandler(getPoolingEngineConfig());
    engHandler.init();

    LegStarAddress address = new LegStarAddress("TheMainframe");
    address.setHostUserID(HOST_USERID);
    address.setHostPassword(HOST_PASSWORD);

    LegStarRequest request = new LegStarRequest("Request01", address, getLsfileaeRequestMessage());
    Client client = new Client(engHandler.getEngine(), "Client01", request);
    wm.schedule(client, new ClientListener());

    Thread.sleep(5000L);
    engHandler.stop();
    executor.shutdownNow();

    assertEquals(LsfileaeCases.getHostBytesHexReply100(),
            HostData.toHexString(request.getResponseMessage().getDataParts().get(0).getContent()));

}

From source file:org.apache.bookkeeper.tools.perf.dlog.PerfReader.java

@Override
protected void execute(Namespace namespace) throws Exception {
    List<Pair<Integer, DistributedLogManager>> managers = new ArrayList<>(flags.numLogs);
    for (int i = 0; i < flags.numLogs; i++) {
        String logName = String.format(flags.logName, i);
        managers.add(Pair.of(i, namespace.openLog(logName)));
    }//from ww w  . j av  a 2 s.  co  m
    log.info("Successfully open {} logs", managers.size());

    // register shutdown hook to aggregate stats
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        isDone.set(true);
        printAggregatedStats(cumulativeRecorder);
    }));

    ExecutorService executor = Executors.newFixedThreadPool(flags.numThreads);
    try {
        for (int i = 0; i < flags.numThreads; i++) {
            final int idx = i;
            final List<DistributedLogManager> logsThisThread = managers.stream()
                    .filter(pair -> pair.getLeft() % flags.numThreads == idx).map(pair -> pair.getRight())
                    .collect(Collectors.toList());
            executor.submit(() -> {
                try {
                    read(logsThisThread);
                } catch (Exception e) {
                    log.error("Encountered error at writing records", e);
                }
            });
        }
        log.info("Started {} write threads", flags.numThreads);
        reportStats();
    } finally {
        executor.shutdown();
        if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
            executor.shutdownNow();
        }
        managers.forEach(manager -> manager.getRight().asyncClose());
    }
}