Java Code Examples for java.util.concurrent.CompletableFuture#delayedExecutor()

The following examples show how to use java.util.concurrent.CompletableFuture#delayedExecutor() . 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: CompletableFutureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
    long timeoutMillis = timeoutMillis();
    // Use an "unreasonably long" long timeout to catch lingering threads
    long longTimeoutMillis = 1000 * 60 * 60 * 24;
    final Executor delayer, longDelayer;
    if (executor == null) {
        delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
        longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
    } else {
        delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
        longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
    }
    long startTime = System.nanoTime();
    CompletableFuture<Integer> f =
        CompletableFuture.supplyAsync(() -> v, delayer);
    CompletableFuture<Integer> g =
        CompletableFuture.supplyAsync(() -> v, longDelayer);

    assertNull(g.getNow(null));

    assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
    long millisElapsed = millisElapsedSince(startTime);
    assertTrue(millisElapsed >= timeoutMillis);
    assertTrue(millisElapsed < LONG_DELAY_MS / 2);

    checkCompletedNormally(f, v);

    checkIncomplete(g);
    assertTrue(g.cancel(true));
}
 
Example 2
Source File: Cache.java    From robozonky with Apache License 2.0 5 votes vote down vote up
default Executor startEviction(Runnable eviction) {
    var executor = CompletableFuture.delayedExecutor(getEvictEvery().toNanos(), TimeUnit.NANOSECONDS);
    executor.execute(() -> {
        try {
            eviction.run();
        } finally {
            executor.execute(eviction);
        }
    });
    return executor;
}
 
Example 3
Source File: CompletableFutureTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
    long timeoutMillis = timeoutMillis();
    // Use an "unreasonably long" long timeout to catch lingering threads
    long longTimeoutMillis = 1000 * 60 * 60 * 24;
    final Executor delayer, longDelayer;
    if (executor == null) {
        delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
        longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
    } else {
        delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
        longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
    }
    long startTime = System.nanoTime();
    CompletableFuture<Integer> f =
        CompletableFuture.supplyAsync(() -> v, delayer);
    CompletableFuture<Integer> g =
        CompletableFuture.supplyAsync(() -> v, longDelayer);

    assertNull(g.getNow(null));

    assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
    long millisElapsed = millisElapsedSince(startTime);
    assertTrue(millisElapsed >= timeoutMillis);
    assertTrue(millisElapsed < LONG_DELAY_MS / 2);

    checkCompletedNormally(f, v);

    checkIncomplete(g);
    assertTrue(g.cancel(true));
}