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.hadoop.hbase.util.RegionMover.java

/**
 * Unload regions from given {@link #hostname} using ack/noAck mode and {@link #maxthreads}.In
 * noAck mode we do not make sure that region is successfully online on the target region
 * server,hence it is best effort.We do not unload regions to hostnames given in
 * {@link #excludeFile}./*from   ww  w  .j  av a 2  s  . c  o m*/
 * @return true if unloading succeeded, false otherwise
 * @throws InterruptedException if the unloader thread was interrupted
 * @throws ExecutionException
 * @throws TimeoutException
 */
public boolean unload() throws InterruptedException, ExecutionException, TimeoutException {
    setConf();
    deleteFile(this.filename);
    ExecutorService unloadPool = Executors.newFixedThreadPool(1);
    Future<Boolean> unloadTask = unloadPool.submit(new Unload(this));
    unloadPool.shutdown();
    try {
        if (!unloadPool.awaitTermination((long) this.timeout, TimeUnit.SECONDS)) {
            LOG.warn("Timed out before finishing the unloading operation. Timeout:" + this.timeout + "sec");
            unloadPool.shutdownNow();
        }
    } catch (InterruptedException e) {
        unloadPool.shutdownNow();
        Thread.currentThread().interrupt();
    }
    try {
        return unloadTask.get(5, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        LOG.warn("Interrupted while unloading Regions from " + this.hostname, e);
        throw e;
    } catch (ExecutionException e) {
        LOG.error("Error while unloading regions from RegionServer " + this.hostname, e);
        throw e;
    }
}

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 {/*from   ww w. ja va  2s.co  m*/
        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:edu.cmu.lti.oaqa.bioqa.providers.kb.TmToolConceptProvider.java

@Override
public List<Concept> getConcepts(List<JCas> jcases) throws AnalysisEngineProcessException {
    // send request
    List<String> normalizedTexts = jcases.stream().map(JCas::getDocumentText)
            .map(PubAnnotationConvertUtil::normalizeText).collect(toList());
    ListMultimap<Integer, PubAnnotation.Denotation> index2denotations = Multimaps
            .synchronizedListMultimap(ArrayListMultimap.create());
    ExecutorService es = Executors.newCachedThreadPool();
    for (String trigger : triggers) {
        es.submit(() -> {/*from w  w w .j a v a2  s  .  c om*/
            try {
                List<String> denotationStrings = requestConcepts(normalizedTexts, trigger);
                assert denotationStrings.size() == jcases.size();
                for (int i = 0; i < jcases.size(); i++) {
                    PubAnnotation.Denotation[] denotations = gson.fromJson(denotationStrings.get(i),
                            PubAnnotation.Denotation[].class);
                    index2denotations.putAll(i, Arrays.asList(denotations));
                }
            } catch (Exception e) {
                throw TmToolConceptProviderException.unknownException(trigger, e);
            }
        });
    }
    es.shutdown();
    try {
        boolean status = es.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        if (!status) {
            throw new AnalysisEngineProcessException();
        }
    } catch (InterruptedException e) {
        throw new AnalysisEngineProcessException(e);
    }
    // convert denotation strings
    List<Concept> concepts = new ArrayList<>();
    for (int i = 0; i < jcases.size(); i++) {
        JCas jcas = jcases.get(i);
        List<PubAnnotation.Denotation> denotations = index2denotations.get(i);
        try {
            concepts.addAll(PubAnnotationConvertUtil.convertDenotationsToConcepts(jcas, denotations));
        } catch (StringIndexOutOfBoundsException e) {
            throw TmToolConceptProviderException.offsetOutOfBounds(jcas.getDocumentText(), denotations, e);
        }
    }
    return concepts;
}

From source file:com.emc.ecs.sync.storage.CasStorageTest.java

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

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

    for (String clipId : clipIds) {
        service.submit(new ClipDeleter(pool, clipId));
    }/*from w  ww  . j  av  a2s  .  c  o m*/

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

    System.out.println();
}

From source file:com.palantir.paxos.PaxosConsensusFastTest.java

@Test
public void loseQuorumDiffToken() throws InterruptedException {
    for (int i = QUORUM_SIZE; i < NUM_POTENTIAL_LEADERS; i++) {
        state.goDown(i);//from ww  w.j  a  v  a2  s  . c om
    }
    LeadershipToken t = state.gainLeadership(0);
    state.goDown(QUORUM_SIZE - 1);
    ExecutorService exec = PTExecutors.newSingleThreadExecutor();
    Future<Void> f = exec.submit(new Callable<Void>() {
        @Override
        public Void call() {
            int i = QUORUM_SIZE - 1;
            while (!Thread.currentThread().isInterrupted()) {
                int next = i + 1;
                if (next == NUM_POTENTIAL_LEADERS) {
                    next = QUORUM_SIZE - 1;
                }
                state.goDown(next);
                state.comeUp(i);
                i = next;
            }
            return null;
        }
    });
    // Don't check leadership immediately after gaining it, since quorum might get lost.
    LeadershipToken token2 = state.gainLeadershipWithoutCheckingAfter(0);
    assertTrue("leader can confirm leadership with quorum", t.sameAs(token2));
    f.cancel(true);
    exec.shutdown();
    exec.awaitTermination(10, TimeUnit.SECONDS);
    for (int i = 0; i < NUM_POTENTIAL_LEADERS; i++) {
        state.comeUp(i);
    }
}

From source file:org.apache.hadoop.fs.FCStatisticsBaseTest.java

@Test(timeout = 70000)
public void testStatisticsThreadLocalDataCleanUp() throws Exception {
    final Statistics stats = new Statistics("test");
    // create a small thread pool to test the statistics
    final int size = 2;
    ExecutorService es = Executors.newFixedThreadPool(size);
    List<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>(size);
    for (int i = 0; i < size; i++) {
        tasks.add(new Callable<Boolean>() {
            public Boolean call() {
                // this populates the data set in statistics
                stats.incrementReadOps(1);
                return true;
            }// w w w.j  a  va2  s .c o m
        });
    }
    // run the threads
    es.invokeAll(tasks);
    // assert that the data size is exactly the number of threads
    final AtomicInteger allDataSize = new AtomicInteger(0);
    allDataSize.set(stats.getAllThreadLocalDataSize());
    Assert.assertEquals(size, allDataSize.get());
    Assert.assertEquals(size, stats.getReadOps());
    // force the GC to collect the threads by shutting down the thread pool
    es.shutdownNow();
    es.awaitTermination(1, TimeUnit.MINUTES);
    es = null;
    System.gc(); // force GC to garbage collect threads

    // wait for up to 60 seconds
    GenericTestUtils.waitFor(new Supplier<Boolean>() {
        @Override
        public Boolean get() {
            int size = stats.getAllThreadLocalDataSize();
            allDataSize.set(size);
            if (size == 0) {
                return true;
            }
            LOG.warn(
                    "not all references have been cleaned up; still " + allDataSize.get() + " references left");
            LOG.warn("triggering another GC");
            System.gc();
            return false;
        }
    }, 500, 60 * 1000);
    Assert.assertEquals(0, allDataSize.get());
    Assert.assertEquals(size, stats.getReadOps());
}

From source file:com.emc.ecs.sync.storage.CasStorageTest.java

private 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 a  v  a  2 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:org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutorOverGraphTest.java

@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldAllowTraversalToIterateInDifferentThreadThanOriginallyEvaluatedWithAutoCommit()
        throws Exception {
    // this test sort of simulates Gremlin Server interaction where a Traversal is eval'd in one Thread, but
    // then iterated in another.  note that Gremlin Server configures the script engine to auto-commit
    // after evaluation.  this basically tests the state of the Gremlin Server GremlinExecutor when
    // being used in sessionless mode
    final ExecutorService evalExecutor = Executors.newSingleThreadExecutor(testingThreadFactory);
    final GremlinExecutor gremlinExecutor = GremlinExecutor.build().afterSuccess(b -> {
        final GraphTraversalSource g = (GraphTraversalSource) b.get("g");
        if (g.getGraph().features().graph().supportsTransactions())
            g.tx().commit();//from   w w w .  j  a v  a2s .  c  o m
    }).executorService(evalExecutor).create();

    final Map<String, Object> bindings = new HashMap<>();
    bindings.put("g", g);

    final AtomicInteger vertexCount = new AtomicInteger(0);

    final ExecutorService iterationExecutor = Executors.newSingleThreadExecutor(testingThreadFactory);
    gremlinExecutor.eval("g.V().out()", bindings).thenAcceptAsync(o -> {
        final Iterator itty = (Iterator) o;
        itty.forEachRemaining(v -> vertexCount.incrementAndGet());
    }, iterationExecutor).join();

    assertEquals(6, vertexCount.get());

    gremlinExecutor.close();
    evalExecutor.shutdown();
    evalExecutor.awaitTermination(30000, TimeUnit.MILLISECONDS);
    iterationExecutor.shutdown();
    iterationExecutor.awaitTermination(30000, TimeUnit.MILLISECONDS);
}

From source file:be.panako.cli.Store.java

@Override
public void run(final String... args) {
    int processors = availableProcessors();
    int counter = 0;
    final ExecutorService executor = Executors.newFixedThreadPool(processors);
    final List<File> files = this.getFilesFromArguments(args);
    boolean extractMetaData = hasArgument("-m", args) || hasArgument("--meta-data", args);
    if (files.size() > 1) {
        String msg = "Processing " + files.size() + " files on " + processors + " seperate threads.";
        System.out.println(msg);/*from  ww  w  .j  ava  2s  .c  om*/
        LOG.info("Store task started. " + msg);
    }
    System.out.println("Audiofile;Audio duration;Fingerprinting duration;ratio");
    for (File file : files) {
        counter++;
        //executor.submit(new StoreTask(file, counter, files.size()));
        StoreTask task = new StoreTask(file, counter, files.size(), extractMetaData);
        task.run();
    }

    try {
        //do not accept more tasks.
        executor.shutdown();
        //wait for tasks to finish
        executor.awaitTermination(300, java.util.concurrent.TimeUnit.DAYS);
        //System.exit(0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    /*catch (InterruptedException e1) {
       //Thread was interrupted
       e1.printStackTrace();
       LOG.severe("Did not finish all tasks, thread was interrupted!");
    }*/
}

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

protected List<String> createTestClips(FPPool pool, int maxBlobSize, int thisMany, Writer summaryWriter)
        throws Exception {
    ExecutorService service = Executors.newFixedThreadPool(CAS_THREADS);

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

    List<String> clipIds = Collections.synchronizedList(new ArrayList<String>());
    List<String> summaries = Collections.synchronizedList(new ArrayList<String>());
    for (int clipIdx = 0; clipIdx < thisMany; clipIdx++) {
        service.submit(new ClipWriter(pool, clipIds, maxBlobSize, summaries));
    }//from   www. j  av a  2s  .  c  o m

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

    Collections.sort(summaries);
    for (String summary : summaries) {
        summaryWriter.append(summary);
    }

    System.out.println();

    return clipIds;
}