Package com.norconex.crawler.core.util
Class ConcurrentUtil
java.lang.Object
com.norconex.crawler.core.util.ConcurrentUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> futures) Combine a list ofCompletableFuture<T>into a singleCompletableFuture<List<T>>.static <T> CompletableFuture<T> callWithAutoShutdown(@NonNull Callable<T> task, @NonNull ExecutorService executor) static voidcleanShutdown(ExecutorService executor) Shuts down an executor service, handlingInterruptedExceptionby interrupting the current thread.static <T> TCallsFuture.get()and wraps thread related exceptions in a runtime CompletionException.static <T> TCallsFuture.get(long, TimeUnit)and wraps thread related exceptions in a runtime CompletionException.static <T> TgetUnderAMinute(Future<T> future) static <T> TgetUnderSecs(Future<T> future, int seconds) static booleanWhether the exception is, or has for cause, an interruption.static CompletableFuture<Void> runWithAutoShutdown(@NonNull Runnable task, @NonNull ExecutorService executor) static voidshutdownAndAwait(ExecutorService executor, Duration maxWait) Shuts down an executor and await for confirmation up to the specified duration, after which a force shutdown is attempted.static <T> CompletableFuture<T> supplyWithAutoShutdown(@NonNull Supplier<T> task, @NonNull ExecutorService executor) static voidwaitUntil(@NonNull BooleanSupplier condition) In some cases we do not want to block any thread when waiting for a future completion.static booleanwaitUntil(@NonNull BooleanSupplier condition, Duration timeout) In some cases we do not want to block any thread when waiting for a future completion.static booleanwaitUntil(@NonNull BooleanSupplier condition, Duration timeout, Duration checkInterval) In some cases we do not want to block any thread when waiting for a future completion.static voidwaitUntilOrThrow(@NonNull BooleanSupplier condition, Duration timeout) In some cases we do not want to block any thread when waiting for a future completion.static voidwaitUntilOrThrow(@NonNull BooleanSupplier condition, Duration timeout, Duration checkInterval) In some cases we do not want to block any thread when waiting for a future completion.static <T> CompletableFuture<T> withAutoShutdown(@NonNull CompletableFuture<T> future, @NonNull ExecutorService executor) static CompletionExceptionWrapsInterruptedException,TimeoutException,ExecutionExceptionroot cause, and any other checked exceptions in a runtimeCompletionException.
-
Method Details
-
runWithAutoShutdown
public static CompletableFuture<Void> runWithAutoShutdown(@NonNull @NonNull Runnable task, @NonNull @NonNull ExecutorService executor) -
supplyWithAutoShutdown
public static <T> CompletableFuture<T> supplyWithAutoShutdown(@NonNull @NonNull Supplier<T> task, @NonNull @NonNull ExecutorService executor) -
callWithAutoShutdown
public static <T> CompletableFuture<T> callWithAutoShutdown(@NonNull @NonNull Callable<T> task, @NonNull @NonNull ExecutorService executor) -
shutdownAndAwait
Shuts down an executor and await for confirmation up to the specified duration, after which a force shutdown is attempted.- Parameters:
executor- the executor to shut downmaxWait- max wait duration before forcing shutdown
-
withAutoShutdown
public static <T> CompletableFuture<T> withAutoShutdown(@NonNull @NonNull CompletableFuture<T> future, @NonNull @NonNull ExecutorService executor) -
get
CallsFuture.get()and wraps thread related exceptions in a runtime CompletionException. SeewrapAsCompletionException(Exception)for more details. Passing anullfuture has no effect and returnsnull.- Parameters:
future- the future to get- Returns:
- the completed future value, if any, or
null
-
get
CallsFuture.get(long, TimeUnit)and wraps thread related exceptions in a runtime CompletionException. SeewrapAsCompletionException(Exception)for more details. Passing anullfuture has no effect and returnsnull.- Type Parameters:
T- future return value- Parameters:
future- the future to gettimeout-unit-- Returns:
- the completed future value, if any, or
null
-
getUnderAMinute
-
getUnderSecs
-
waitUntil
In some cases we do not want to block any thread when waiting for a future completion. This method instead loops (with a tiny delay) until the supplier returnstrue.- Parameters:
condition- condition evaluated (trueto exit the loop)
-
waitUntil
In some cases we do not want to block any thread when waiting for a future completion. This method instead loops (with a tiny delay) until the supplier returnstrue. If a timeout is specified the method will return once the time out is reached, with afalsevalue.- Parameters:
condition- condition evaluated (trueto exit the loop)timeout- optional duration after which to throw- Returns:
trueif returning under the specified timeout or if timeout isnull.
-
waitUntil
public static boolean waitUntil(@NonNull @NonNull BooleanSupplier condition, Duration timeout, Duration checkInterval) In some cases we do not want to block any thread when waiting for a future completion. This method instead loops (with a tiny delay) until the supplier returnstrue. If a timeout is specified the method will return once the time out is reached, with afalsevalue.- Parameters:
condition- condition evaluated (trueto exit the loop)timeout- optional duration after which to throwcheckInterval- the amount of time to wait between each condition check- Returns:
trueif returning under the specified timeout or if timeout isnull.
-
waitUntilOrThrow
public static void waitUntilOrThrow(@NonNull @NonNull BooleanSupplier condition, Duration timeout) throws TimeoutException In some cases we do not want to block any thread when waiting for a future completion. This method instead loops (with a tiny delay) until the supplier returnstrue. If a timeout is specified the method will throw if it waited for more time than the timeout duration.- Parameters:
condition- condition evaluated (trueto exit the loop)timeout- optional duration after which to throw- Throws:
TimeoutException- when waiting for more than specified timeout.
-
waitUntilOrThrow
public static void waitUntilOrThrow(@NonNull @NonNull BooleanSupplier condition, Duration timeout, Duration checkInterval) throws TimeoutException In some cases we do not want to block any thread when waiting for a future completion. This method instead loops (with a tiny delay) until the supplier returnstrue. If a timeout is specified the method will throw if it waited for more time than the timeout duration.- Parameters:
condition- condition evaluated (trueto exit the loop)timeout- optional duration after which to throwcheckInterval- the amount of time to wait between each condition check- Throws:
TimeoutException- when waiting for more than specified timeout.
-
cleanShutdown
Shuts down an executor service, handlingInterruptedExceptionby interrupting the current thread. Returns aCompletionExceptionon failures.- Parameters:
executor- the executor to shut down (can be null)
-
allOf
Combine a list ofCompletableFuture<T>into a singleCompletableFuture<List<T>>.- Type Parameters:
T- type of future return values- Parameters:
futures- list of completable future- Returns:
- single completable future
-
wrapAsCompletionException
WrapsInterruptedException,TimeoutException,ExecutionExceptionroot cause, and any other checked exceptions in a runtimeCompletionException. TheInterruptedExceptionis handled gracefully- Parameters:
e- the original exception- Returns:
- the wrapping
CompletionException
-
isInterruption
Whether the exception is, or has for cause, an interruption.- Parameters:
t- exception- Returns:
trueif an interruption exception
-