Package com.illumon.util
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 Summary
Constructors Constructor Description FutureUtils() -
Method Summary
Modifier and Type Method Description static ThreaddaemonThread(Runnable target)Create a thread and make it a daemon.static <V> FutureTask<V>possiblyStartFutureThread(FutureTask<V> futureTask)If the provided FutureTask is not null, start it running in a new thread.static <V> VpossiblyWaitForFuture(Future<V> future, String objectName)If the provided Future is not null, wait for it to complete and return the result.static ThreadstartDaemonThread(Runnable target)Create a daemon thread and start it.static <V> VwaitForFutureAndSetException(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- anAtomicReferencethat will be set to anExecutionExceptionif one occurs- Returns:
- the Future's computed result, or null if future is null or an exception occurs
-
daemonThread
Create a thread and make it a daemon.- Parameters:
target- the runnable to pass the new Thread- Returns:
- the created thread
-
startDaemonThread
Create a daemon thread and start it.- Parameters:
target- the runnable to pass the new Thread- Returns:
- the created thread, started
-