Class ConcurrentUtil

java.lang.Object
com.norconex.crawler.core.util.ConcurrentUtil

public final class ConcurrentUtil extends Object
  • 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

      public static void shutdownAndAwait(ExecutorService executor, Duration maxWait)
      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 down
      maxWait - max wait duration before forcing shutdown
    • withAutoShutdown

      public static <T> CompletableFuture<T> withAutoShutdown(@NonNull @NonNull CompletableFuture<T> future, @NonNull @NonNull ExecutorService executor)
    • get

      public static <T> T get(Future<T> future)
      Calls Future.get() and wraps thread related exceptions in a runtime CompletionException. See wrapAsCompletionException(Exception) for more details. Passing a null future has no effect and returns null.
      Parameters:
      future - the future to get
      Returns:
      the completed future value, if any, or null
    • get

      public static <T> T get(Future<T> future, long timeout, TimeUnit unit)
      Calls Future.get(long, TimeUnit) and wraps thread related exceptions in a runtime CompletionException. See wrapAsCompletionException(Exception) for more details. Passing a null future has no effect and returns null.
      Type Parameters:
      T - future return value
      Parameters:
      future - the future to get
      timeout -
      unit -
      Returns:
      the completed future value, if any, or null
    • getUnderAMinute

      public static <T> T getUnderAMinute(Future<T> future)
    • getUnderSecs

      public static <T> T getUnderSecs(Future<T> future, int seconds)
    • waitUntil

      public static void waitUntil(@NonNull @NonNull BooleanSupplier condition)
      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 returns true.
      Parameters:
      condition - condition evaluated (true to exit the loop)
    • waitUntil

      public static boolean waitUntil(@NonNull @NonNull BooleanSupplier condition, Duration timeout)
      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 returns true. If a timeout is specified the method will return once the time out is reached, with a false value.
      Parameters:
      condition - condition evaluated (true to exit the loop)
      timeout - optional duration after which to throw
      Returns:
      true if returning under the specified timeout or if timeout is null.
    • 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 returns true. If a timeout is specified the method will return once the time out is reached, with a false value.
      Parameters:
      condition - condition evaluated (true to exit the loop)
      timeout - optional duration after which to throw
      checkInterval - the amount of time to wait between each condition check
      Returns:
      true if returning under the specified timeout or if timeout is null.
    • 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 returns true. If a timeout is specified the method will throw if it waited for more time than the timeout duration.
      Parameters:
      condition - condition evaluated (true to 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 returns true. If a timeout is specified the method will throw if it waited for more time than the timeout duration.
      Parameters:
      condition - condition evaluated (true to exit the loop)
      timeout - optional duration after which to throw
      checkInterval - the amount of time to wait between each condition check
      Throws:
      TimeoutException - when waiting for more than specified timeout.
    • cleanShutdown

      public static void cleanShutdown(ExecutorService executor)
      Shuts down an executor service, handling InterruptedException by interrupting the current thread. Returns a CompletionException on failures.
      Parameters:
      executor - the executor to shut down (can be null)
    • allOf

      public static <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> futures)
      Combine a list of CompletableFuture<T> into a single CompletableFuture<List<T>>.
      Type Parameters:
      T - type of future return values
      Parameters:
      futures - list of completable future
      Returns:
      single completable future
    • wrapAsCompletionException

      public static CompletionException wrapAsCompletionException(Exception e)
      Wraps InterruptedException, TimeoutException, ExecutionException root cause, and any other checked exceptions in a runtime CompletionException. The InterruptedException is handled gracefully
      Parameters:
      e - the original exception
      Returns:
      the wrapping CompletionException
    • isInterruption

      public static boolean isInterruption(Throwable t)
      Whether the exception is, or has for cause, an interruption.
      Parameters:
      t - exception
      Returns:
      true if an interruption exception