com.arangodb.async.internal.utils.CompletableFutureUtils Maven / Gradle / Ivy
package com.arangodb.async.internal.utils;
import java.util.concurrent.*;
public class CompletableFutureUtils {
private static final ScheduledExecutorService timeoutScheduler = Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
}
);
private CompletableFutureUtils() {
}
public static CompletableFuture orTimeout(CompletableFuture completableFuture, long timeout,
TimeUnit unit) {
ScheduledFuture> timeoutTask = timeoutScheduler.schedule(() ->
completableFuture.completeExceptionally(new TimeoutException()), timeout, unit);
completableFuture.whenComplete((v, e) -> timeoutTask.cancel(false));
return completableFuture;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy