Package com.illumon.util
Class FutureUtils
java.lang.Object
com.illumon.util.FutureUtils
Utility class for use to assist with Future-related tasks.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <V> FutureTask<V>
possiblyStartFutureThread
(FutureTask<V> futureTask) If the provided FutureTask is not null, start it running in a new thread.static <V> V
possiblyWaitForFuture
(Future<V> future, String objectName) If the provided Future is not null, wait for it to complete and return the result.static <V> V
waitForFutureAndSetException
(Future<V> future, AtomicReference<ExecutionException> exceptionReference) If the provided Future is not null, wait for it to complete and return the result.
-
Constructor Details
-
FutureUtils
public FutureUtils()
-
-
Method Details
-
possiblyStartFutureThread
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
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 nullobjectName
- 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 nullexceptionReference
- anAtomicReference
that will be set to anExecutionException
if one occurs- Returns:
- the Future's computed result, or null if future is null or an exception occurs
-