Class FutureUtils

java.lang.Object
com.illumon.util.FutureUtils

public class FutureUtils extends Object
Utility class for use to assist with Future-related tasks.
  • Constructor Details

    • FutureUtils

      public FutureUtils()
  • Method Details

    • possiblyStartFutureThread

      public static <V> FutureTask<V> possiblyStartFutureThread(@Nullable FutureTask<V> futureTask)
      If the provided FutureTask is not null, start it running in a new thread.
      Parameters:
      futureTask - the FutureTask to be started, or null
      Returns:
      the futureTask
    • possiblyWaitForFuture

      public static <V> V possiblyWaitForFuture(@Nullable Future<V> future, @NotNull String objectName)
      If the provided Future is not null, wait for it to complete and return the result. If the Future is null, return null immediately. A non-null Future should have been started before calling this method. Any ExecutionException will be wrapped up in a RuntimeException.
      Type Parameters:
      V - the result type returned by this Future's get method
      Parameters:
      future - the Future, or null
      objectName - a name for the future to be used if an exception needs to be rethrown
      Returns:
      the Future's computed result, or null if future is null
    • waitForFutureAndSetException

      public static <V> V waitForFutureAndSetException(@Nullable Future<V> future, AtomicReference<ExecutionException> exceptionReference)
      If the provided Future is not null, wait for it to complete and return the result. If the Future is null, return null immediately. A non-null Future should have been started before calling this method.
      Type Parameters:
      V - the result type returned by this Future's get method
      Parameters:
      future - the Future, or null
      exceptionReference - an AtomicReference that will be set to an ExecutionException if one occurs
      Returns:
      the Future's computed result, or null if future is null or an exception occurs
    • daemonThread

      public static Thread daemonThread(@NotNull Runnable target)
      Create a thread and make it a daemon.
      Parameters:
      target - the runnable to pass the new Thread
      Returns:
      the created thread
    • startDaemonThread

      public static Thread startDaemonThread(@NotNull Runnable target)
      Create a daemon thread and start it.
      Parameters:
      target - the runnable to pass the new Thread
      Returns:
      the created thread, started