Java Code Examples for java.util.concurrent.ExecutorCompletionService#poll()

The following examples show how to use java.util.concurrent.ExecutorCompletionService#poll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TStreamTest.java    From quarks with Apache License 2.0 6 votes vote down vote up
private void waitForCompletion(ExecutorCompletionService<Boolean> completer, int numtasks) throws ExecutionException {
    int remainingTasks = numtasks;
    while (remainingTasks > 0) {
        try {
            Future<Boolean> completed = completer.poll(4, TimeUnit.SECONDS);
            if (completed == null) {
                System.err.println("Completer timed out");
                throw new RuntimeException(new TimeoutException());
            }
            else {
                completed.get();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        remainingTasks--;
    }
}
 
Example 2
Source File: ExecutorCompletionServiceTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * If poll returns non-null, the returned task is completed
 */
public void testPoll1() throws Exception {
    final ExecutorService e = Executors.newCachedThreadPool();
    final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
    try (PoolCleaner cleaner = cleaner(e)) {
        assertNull(ecs.poll());
        Callable c = new StringTask();
        ecs.submit(c);

        long startTime = System.nanoTime();
        Future f;
        while ((f = ecs.poll()) == null) {
            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
                fail("timed out");
            Thread.yield();
        }
        assertTrue(f.isDone());
        assertSame(TEST_STRING, f.get());
    }
}
 
Example 3
Source File: ExecutorCompletionServiceTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * If timed poll returns non-null, the returned task is completed
 */
public void testPoll2() throws InterruptedException {
    final ExecutorService e = Executors.newCachedThreadPool();
    final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
    try (PoolCleaner cleaner = cleaner(e)) {
        assertNull(ecs.poll());
        Callable c = new StringTask();
        ecs.submit(c);
        Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS);
        if (f != null)
            assertTrue(f.isDone());
    }
}
 
Example 4
Source File: CompareAnalyzers.java    From quaerite with Apache License 2.0 4 votes vote down vote up
public Map<String, EquivalenceSet> compare(SearchClient client,
                                           String baseField,
                                           String filteredField) {

    ArrayBlockingQueue<Set<TokenDF>> queue = new ArrayBlockingQueue<>(100);
    List<ReAnalyzer> reAnalyzers = new ArrayList<>();
    for (int i = 0; i < numThreads; i++) {
        reAnalyzers.add(new ReAnalyzer(queue, client, filteredField));
    }

    ExecutorService executorService = Executors.newFixedThreadPool(numThreads + 1);
    ExecutorCompletionService<Integer> completionService =
            new ExecutorCompletionService<>(executorService);
    completionService.submit(new TermGetter(queue, numThreads, client, baseField));
    for (int i = 0; i < numThreads; i++) {
        completionService.submit(reAnalyzers.get(i));
    }
    //map
    int completed = 0;
    int totalAnalyzed = 0;
    while (completed < numThreads + 1) {
        try {
            Future<Integer> future = completionService.poll(1, TimeUnit.SECONDS);
            if (future != null) {
                int analyzed = future.get();
                if (analyzed > 0) {
                    totalAnalyzed += analyzed;
                }
                completed++;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    LOG.info("Analyzed " + totalAnalyzed);
    executorService.shutdownNow();
    //reduce
    Map<String, EquivalenceSet> overall = new HashMap<>();
    for (ReAnalyzer reAnalyzer : reAnalyzers) {
        reduce(reAnalyzer.getMap(), overall);
    }

    return MapUtil.sortByDescendingValue(overall);
}