Class ConcurrencyUtil

java.lang.Object
es.urjc.etsii.grafo.util.ConcurrencyUtil

public class ConcurrencyUtil extends Object
Helper methods to deal with concurrent tasks
  • Constructor Details

    • ConcurrencyUtil

      public ConcurrencyUtil()
  • Method Details

    • await

      public static void await(ExecutorService executor)
      Awaits termination for the given executor service. Wraps InterruptedException in an unchecked RuntimeException
      Parameters:
      executor - Executor service
    • await

      public static <T> T await(Future<T> f)
      Block until the task is completed. Wraps the annoying checked exception.
      Type Parameters:
      T - Future type
      Parameters:
      f - Future we will wait for
      Returns:
      SimplifiedResult of the task
      Throws:
      RuntimeException - in case any error happened during the execution
    • await

      public static <T> Optional<T> await(Future<T> f, Consumer<Exception> exceptionHandler)
      Block until the task is completed. Handles the exception with the given handler
      Type Parameters:
      T - Optional type
      Parameters:
      f - Future we will wait for
      exceptionHandler - pass the exception to handler instead of promoting to RuntimeException
      Returns:
      SimplifiedResult of the task
      Throws:
      RuntimeException - in case any error happened during the execution
    • awaitAll

      public static <T> List<T> awaitAll(Collection<Future<T>> futures)
      Await a collection of futures
      Type Parameters:
      T - Futures type
      Parameters:
      futures - collection of futures
      Returns:
      Objects inside futures
    • awaitAll

      public static <T> List<T> awaitAll(Stream<Future<T>> futures)
      Await a stream of futures
      Type Parameters:
      T - Futures type
      Parameters:
      futures - stream of futures
      Returns:
      Objects inside futures
    • sleep

      public static void sleep(long time, TimeUnit unit)
      Sleep without having to deal with InterruptedException
      Parameters:
      time - time to sleep
      unit - unit of time