java.util.concurrent.CompletionService

Here are the examples of the java api class java.util.concurrent.CompletionService taken from open source projects.

1. ExecutorCompletionServiceTest#testPollReturnsNull()

Project: openjdk
File: ExecutorCompletionServiceTest.java
/**
     * poll returns null before the returned task is completed
     */
public void testPollReturnsNull() throws InterruptedException, ExecutionException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    final CountDownLatch proceed = new CountDownLatch(1);
    cs.submit(new Callable() {

        public String call() throws Exception {
            proceed.await();
            return TEST_STRING;
        }
    });
    assertNull(cs.poll());
    assertNull(cs.poll(0L, MILLISECONDS));
    assertNull(cs.poll(Long.MIN_VALUE, MILLISECONDS));
    long startTime = System.nanoTime();
    assertNull(cs.poll(timeoutMillis(), MILLISECONDS));
    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
    proceed.countDown();
    assertSame(TEST_STRING, cs.take().get());
}

2. ExecutorCompletionServiceTest#testPoll2()

Project: openjdk
File: ExecutorCompletionServiceTest.java
/**
     * timed poll returns non-null when the returned task is completed
     */
public void testPoll2() throws InterruptedException, ExecutionException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    assertNull(cs.poll());
    cs.submit(new StringTask());
    long startTime = System.nanoTime();
    Future f;
    while ((f = cs.poll(SHORT_DELAY_MS, MILLISECONDS)) == null) {
        if (millisElapsedSince(startTime) > LONG_DELAY_MS)
            fail("timed out");
        Thread.yield();
    }
    assertTrue(f.isDone());
    assertSame(TEST_STRING, f.get());
}

3. ExecutorCompletionServiceTest#testPoll1()

Project: openjdk
File: ExecutorCompletionServiceTest.java
/**
     * poll returns non-null when the returned task is completed
     */
public void testPoll1() throws InterruptedException, ExecutionException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    assertNull(cs.poll());
    cs.submit(new StringTask());
    long startTime = System.nanoTime();
    Future f;
    while ((f = cs.poll()) == null) {
        if (millisElapsedSince(startTime) > LONG_DELAY_MS)
            fail("timed out");
        Thread.yield();
    }
    assertTrue(f.isDone());
    assertSame(TEST_STRING, f.get());
}

4. ExecutorCompletionServiceTest#testTaskAssortment()

Project: openjdk
File: ExecutorCompletionServiceTest.java
/**
     * successful and failed tasks are both returned
     */
public void testTaskAssortment() throws InterruptedException, ExecutionException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    ArithmeticException ex = new ArithmeticException();
    for (int i = 0; i < 2; i++) {
        cs.submit(new StringTask());
        cs.submit(callableThrowing(ex));
        cs.submit(runnableThrowing(ex), null);
    }
    int normalCompletions = 0;
    int exceptionalCompletions = 0;
    for (int i = 0; i < 3 * 2; i++) {
        try {
            if (cs.take().get() == TEST_STRING)
                normalCompletions++;
        } catch (ExecutionException expected) {
            assertTrue(expected.getCause() instanceof ArithmeticException);
            exceptionalCompletions++;
        }
    }
    assertEquals(2 * 1, normalCompletions);
    assertEquals(2 * 2, exceptionalCompletions);
    assertNull(cs.poll());
}

5. ExecutorCompletionServiceTest#testTake()

Project: openjdk
File: ExecutorCompletionServiceTest.java
/**
     * A taken submitted task is completed
     */
public void testTake() throws InterruptedException, ExecutionException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    cs.submit(new StringTask());
    Future f = cs.take();
    assertTrue(f.isDone());
    assertSame(TEST_STRING, f.get());
}

6. ExecutorCompletionServiceTest#testTake2()

Project: openjdk
File: ExecutorCompletionServiceTest.java
/**
     * Take returns the same future object returned by submit
     */
public void testTake2() throws InterruptedException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    Future f1 = cs.submit(new StringTask());
    Future f2 = cs.take();
    assertSame(f1, f2);
}

7. ExecutorCompletionServiceTest#testSubmitNullRunnable()

Project: openjdk
File: ExecutorCompletionServiceTest.java
/**
     * ecs.submit(null, val) throws NullPointerException
     */
public void testSubmitNullRunnable() {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    try {
        cs.submit((Runnable) null, Boolean.TRUE);
        shouldThrow();
    } catch (NullPointerException success) {
    }
}

8. ExecutorCompletionServiceTest#testSubmitNullCallable()

Project: openjdk
File: ExecutorCompletionServiceTest.java
/**
     * ecs.submit(null) throws NullPointerException
     */
public void testSubmitNullCallable() {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    try {
        cs.submit((Callable) null);
        shouldThrow();
    } catch (NullPointerException success) {
    }
}