Class WaitForAsyncUtils
General Convention (Method Names)
asyncmethods without a suffix refer to some unknown thread in a thread pool.- Methods ending with the suffix
Fxrefer to the "FX application thread".
Exception Handling
As exceptions on different threads are thrown the caller is usually not aware
of these exceptions. Exceptions returned directly from this framework are wrapped
in RuntimeExceptions.
There are two ways this class notifies the user of exceptions:
- The returned
Future. - The internal exception stack.
Usually exceptions are forwarded to the Future returned by the methods
of this class. When the calling thread calls Future.get() on the Future
any exceptions encountered during execution will be thrown. All waitFor methods
acquire the value of the Future and accordingly throw the same exceptions.
The internal exception stack stores all unhandled exceptions thrown during
direct calls to the async methods. As this class can not guarantee that
exceptions in these methods are handled properly, it will internally store
these exceptions. The exceptions will be in the stack, until they are handled
somewhere in the application. If the field autoCheckException is set to
true, any subsequent calls to one of the async methods will
throw one of those exceptions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic booleanIf true any call to anasyncmethod will check for the occurrence of unhandled exceptions.static booleanIf true any call to anasyncmethod will check for the occurrence of unhandled exceptions in anyThread.static booleanIf true any exceptions encountered during execution of theasyncmethods will be printed to stderr. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Future<T> static <T> Future<T> static <T> Future<T> static voidChecks if an exception in an async task occurred that has not been checked currently.static voidClears all unhandled exceptions.static voidSleeps the current thread for the given duration.static voidWaits for givenCallableto return true otherwise times out with aTimeoutException.static <T> Tstatic voidWaits for givenObservableBooleanValueto return true otherwise times out with aTimeoutException.static <T> TWaits for the givenFutureto be set and then returns the future result of typeT.static voidwaitForAsync(long millis, Runnable runnable) Runs the givenRunnableon a newThreadand waitsmillismilliseconds for it to finish, otherwise times out with aTimeoutException.static <T> TwaitForAsync(long millis, Callable<T> callable) static voidwaitForAsyncFx(long millis, Runnable runnable) Runs the givenRunnableon the JavaFX Application Thread at some unspecified time in the future and waitsmillismilliseconds for it to finish, otherwise times out with aTimeoutException.static <T> TwaitForAsyncFx(long millis, Callable<T> callable) Calls the givenCallableon the JavaFX Application Thread at some unspecified time in the future and waitsmillismilliseconds for it to finish.static voidWaits for the event queue of the "JavaFX Application Thread" to be completed, as well as any new events triggered in it.static voidwaitForFxEvents(int attemptsCount) Waits up toattemptsCountattempts for the event queue of the "JavaFX Application Thread" to be completed, as well as any new events triggered on it.
-
Field Details
-
printException
public static boolean printExceptionIf true any exceptions encountered during execution of theasyncmethods will be printed to stderr. The exceptions will be printed at the time they occur (not when fetched). -
autoCheckException
public static boolean autoCheckExceptionIf true any call to anasyncmethod will check for the occurrence of unhandled exceptions. -
checkAllExceptions
public static boolean checkAllExceptionsIf true any call to anasyncmethod will check for the occurrence of unhandled exceptions in anyThread.
-
-
Constructor Details
-
WaitForAsyncUtils
public WaitForAsyncUtils()
-
-
Method Details
-
async
Runs the givenRunnableon a newThreadand returns aFuturethat is set on finish or error.You need to evaluate the returned
Futurevia (Future.get()) for exceptions or call thecheckException()method to handle exceptions after the task has finished.- Parameters:
runnable- theRunnableto run- Returns:
- the
Futureresult of theRunnable
-
async
Runs the givenRunnableon a newThreadand returns aFuturethat is set on finish or error.You need to evaluate the returned
Futurevia (Future.get()) for exceptions or call thecheckException()method to handle exceptions after the task has finished.- Parameters:
runnable- theRunnableto runthrowExceptions- whether or not to throw exceptions on the executing thread- Returns:
- the
Futureresult of the runnable
-
async
Calls the givenCallableon a newThreadand returns aFuturethat is set on finish or error.You need to evaluate the returned
Futurevia (Future.get()) for exceptions or call thecheckException()method to handle exceptions after the task has finished.- Type Parameters:
T- the return type of theCallable- Parameters:
callable- theCallableto run- Returns:
- the
Futureresult of theCallable
-
async
Calls the givenCallableon a newThreadand returns aFuturethat is set on finish or error.You need to evaluate the returned
Futurevia (Future.get()) for exceptions or call thecheckException()method to handle exceptions after the task has finished.- Type Parameters:
T- the return type of theCallable- Parameters:
callable- theCallableto runthrowExceptions- whether or not to throw exceptions on the executing thread- Returns:
- the
Futureresult of theCallable
-
asyncFx
Runs the givenRunnableon the JavaFX Application Thread at some unspecified time in the future and returns aFuturethat is set on finish or error.You need to evaluate the returned
Futurevia (Future.get()) for exceptions or call thecheckException()method to handle exceptions after the task has finished.- Parameters:
runnable- theRunnableto run- Returns:
- the
Futureresult of theRunnable
-
asyncFx
Calls the givenCallableon the JavaFX Application Thread at some unspecified time in the future and returns aFuturethat is set on finish or error.You need to evaluate the returned
Futurevia (Future.get()) for exceptions or call thecheckException()method to handle exceptions after the task has finished.- Type Parameters:
T- theCallabletype- Parameters:
callable- theCallable- Returns:
- the
Futureresult of theCallable
-
waitFor
-
waitFor
public static <T> T waitFor(long timeout, TimeUnit timeUnit, Future<T> future) throws TimeoutException - Type Parameters:
T- the type of theFuture- Parameters:
timeout- the timeout to wait fortimeUnit- the time unittimeoutis infuture- theFutureto wait for to be set- Returns:
- the result of the
Future - Throws:
TimeoutException- if the wait timed out
-
waitFor
public static void waitFor(long timeout, TimeUnit timeUnit, Callable<Boolean> condition) throws TimeoutException Waits for givenCallableto return true otherwise times out with aTimeoutException. The condition will be evaluated at least once. This method will wait for the last condition to finish after a timeout.- Parameters:
timeout- the timeout to wait fortimeUnit- the time unittimeoutis incondition- the condition to wait for to be true- Throws:
TimeoutException- if the wait timed out
-
waitFor
public static void waitFor(long timeout, TimeUnit timeUnit, javafx.beans.value.ObservableBooleanValue booleanValue) throws TimeoutException Waits for givenObservableBooleanValueto return true otherwise times out with aTimeoutException.- Parameters:
timeout- the timeout to wait fortimeUnit- the time unittimeoutis inbooleanValue- the observable- Throws:
TimeoutException- if the wait timed out
-
waitForFxEvents
public static void waitForFxEvents()Waits for the event queue of the "JavaFX Application Thread" to be completed, as well as any new events triggered in it. -
waitForFxEvents
public static void waitForFxEvents(int attemptsCount) Waits up toattemptsCountattempts for the event queue of the "JavaFX Application Thread" to be completed, as well as any new events triggered on it.- Parameters:
attemptsCount- the number of attempts to try
-
sleep
Sleeps the current thread for the given duration.- Parameters:
duration- the duration to sleeptimeUnit- the time unitdurationis in
-
waitForAsync
Runs the givenRunnableon a newThreadand waitsmillismilliseconds for it to finish, otherwise times out with aTimeoutException.- Parameters:
millis- number of milliseconds to waitrunnable- theRunnableto run
-
waitForAsync
Calls the givenCallableon a newThreadand waitsmillismilliseconds for it to finish. If finished, returns the future result of typeT, otherwise times out with aTimeoutException.- Type Parameters:
T- the type returned by theCallable- Parameters:
millis- number of milliseconds to waitcallable- theCallableto call- Returns:
- the result returned by the
Callable
-
waitForAsyncFx
Runs the givenRunnableon the JavaFX Application Thread at some unspecified time in the future and waitsmillismilliseconds for it to finish, otherwise times out with aTimeoutException.- Parameters:
millis- number of milliseconds to waitrunnable- theRunnableto run
-
waitForAsyncFx
Calls the givenCallableon the JavaFX Application Thread at some unspecified time in the future and waitsmillismilliseconds for it to finish. If finished, returnsTotherwise times out with aTimeoutException.- Type Parameters:
T- the type returned by theCallable- Parameters:
millis- number of milliseconds to waitcallable- theCallableto call- Returns:
- the result returned by the
Callable
-
checkException
-
clearExceptions
public static void clearExceptions()Clears all unhandled exceptions.
-