All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arangodb.vst.internal.utils.CompletableFutureUtils Maven / Gradle / Ivy

There is a newer version: 7.15.0
Show newest version
package com.arangodb.vst.internal.utils;

import java.util.concurrent.*;

public class CompletableFutureUtils {

    private CompletableFutureUtils() {
    }

    private static final ScheduledExecutorService timeoutScheduler = Executors.newSingleThreadScheduledExecutor(r -> {
                Thread t = Executors.defaultThreadFactory().newThread(r);
                t.setDaemon(true);
                return t;
            }
    );

    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 - 2024 Weber Informatics LLC | Privacy Policy