public final class WaitForAsyncUtils
extends java.lang.Object
General Convention (Method Names)
async methods without a suffix refer to some unknown thread in a thread pool.
Fx refer 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:
Future.
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.
| Modifier and Type | Field and Description |
|---|---|
static boolean |
autoCheckException
If true any call to an
async method will check for
the occurrence of unhandled exceptions. |
static boolean |
checkAllExceptions
If true any call to an
async method will check for
the occurrence of unhandled exceptions in any Thread. |
static boolean |
printException
If true any exceptions encountered during execution of the
async methods will be printed to stderr. |
| Constructor and Description |
|---|
WaitForAsyncUtils() |
| Modifier and Type | Method and Description |
|---|---|
static <T> java.util.concurrent.Future<T> |
async(java.util.concurrent.Callable<T> callable)
Calls the given
Callable on a new Thread and returns a
Future that is set on finish or error. |
static <T> java.util.concurrent.Future<T> |
async(java.util.concurrent.Callable<T> callable,
boolean throwExceptions)
Calls the given
Callable on a new Thread and returns a
Future that is set on finish or error. |
static java.util.concurrent.Future<java.lang.Void> |
async(java.lang.Runnable runnable)
Runs the given
Runnable on a new Thread and returns a
Future that is set on finish or error. |
static java.util.concurrent.Future<java.lang.Void> |
async(java.lang.Runnable runnable,
boolean throwExceptions)
Runs the given
Runnable on a new Thread and returns a
Future that is set on finish or error. |
static <T> java.util.concurrent.Future<T> |
asyncFx(java.util.concurrent.Callable<T> callable)
Calls the given
Callable on the JavaFX Application Thread at some
unspecified time in the future and returns a Future that is set
on finish or error. |
static java.util.concurrent.Future<java.lang.Void> |
asyncFx(java.lang.Runnable runnable)
Runs the given
Runnable on the JavaFX Application Thread at some
unspecified time in the future and returns a Future that is set
on finish or error. |
static void |
checkException()
Checks if an exception in an async task occurred that has not been checked currently.
|
static void |
clearExceptions()
Clears all unhandled exceptions.
|
static void |
sleep(long duration,
java.util.concurrent.TimeUnit timeUnit)
Sleeps the current thread for the given duration.
|
static <T> T |
waitFor(java.util.concurrent.Future<T> future)
Waits for the given
Future to be set and then returns the
future result of type T. |
static void |
waitFor(long timeout,
java.util.concurrent.TimeUnit timeUnit,
java.util.concurrent.Callable<java.lang.Boolean> condition)
Waits for given
Callable to return true otherwise times out with
a TimeoutException. |
static <T> T |
waitFor(long timeout,
java.util.concurrent.TimeUnit timeUnit,
java.util.concurrent.Future<T> future)
Waits for given
Future to be set and returns T, otherwise times out
with a TimeoutException. |
static void |
waitFor(long timeout,
java.util.concurrent.TimeUnit timeUnit,
javafx.beans.value.ObservableBooleanValue booleanValue)
Waits for given
ObservableBooleanValue to return true otherwise
times out with a TimeoutException. |
static <T> T |
waitForAsync(long millis,
java.util.concurrent.Callable<T> callable)
Calls the given
Callable on a new Thread and waits millis
milliseconds for it to finish. |
static void |
waitForAsync(long millis,
java.lang.Runnable runnable)
Runs the given
Runnable on a new Thread and waits millis
milliseconds for it to finish, otherwise times out with a TimeoutException. |
static <T> T |
waitForAsyncFx(long millis,
java.util.concurrent.Callable<T> callable)
Calls the given
Callable on the JavaFX Application Thread at some unspecified time
in the future and waits millis milliseconds for it to finish. |
static void |
waitForAsyncFx(long millis,
java.lang.Runnable runnable)
Runs the given
Runnable on the JavaFX Application Thread at some unspecified time
in the future and waits millis milliseconds for it to finish, otherwise times out with
a TimeoutException. |
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.
|
static void |
waitForFxEvents(int attemptsCount)
Waits up to
attemptsCount attempts for the event queue of the
"JavaFX Application Thread" to be completed, as well as any new events
triggered on it. |
public static boolean printException
async methods will be printed to stderr.
The exceptions will be printed at the time they occur (not when fetched).public static boolean autoCheckException
async method will check for
the occurrence of unhandled exceptions.public static boolean checkAllExceptions
async method will check for
the occurrence of unhandled exceptions in any Thread.public static java.util.concurrent.Future<java.lang.Void> async(java.lang.Runnable runnable)
Runnable on a new Thread and returns a
Future that is set on finish or error.
You need to evaluate the returned Future via (Future.get())
for exceptions or call the checkException() method to handle exceptions
after the task has finished.
runnable - the Runnable to runFuture result of the Runnablepublic static java.util.concurrent.Future<java.lang.Void> async(java.lang.Runnable runnable,
boolean throwExceptions)
Runnable on a new Thread and returns a
Future that is set on finish or error.
You need to evaluate the returned Future via (Future.get())
for exceptions or call the checkException() method to handle exceptions
after the task has finished.
runnable - the Runnable to runthrowExceptions - whether or not to throw exceptions on the executing
threadFuture result of the runnablepublic static <T> java.util.concurrent.Future<T> async(java.util.concurrent.Callable<T> callable)
Callable on a new Thread and returns a
Future that is set on finish or error.
You need to evaluate the returned Future via (Future.get())
for exceptions or call the checkException() method to handle exceptions
after the task has finished.
T - the return type of the Callablecallable - the Callable to runFuture result of the Callablepublic static <T> java.util.concurrent.Future<T> async(java.util.concurrent.Callable<T> callable,
boolean throwExceptions)
Callable on a new Thread and returns a
Future that is set on finish or error.
You need to evaluate the returned Future via (Future.get())
for exceptions or call the checkException() method to handle exceptions
after the task has finished.
T - the return type of the Callablecallable - the Callable to runthrowExceptions - whether or not to throw exceptions on the executing
threadFuture result of the Callablepublic static java.util.concurrent.Future<java.lang.Void> asyncFx(java.lang.Runnable runnable)
Runnable on the JavaFX Application Thread at some
unspecified time in the future and returns a Future that is set
on finish or error.
You need to evaluate the returned Future via (Future.get())
for exceptions or call the checkException() method to handle
exceptions after the task has finished.
runnable - the Runnable to runFuture result of the Runnablepublic static <T> java.util.concurrent.Future<T> asyncFx(java.util.concurrent.Callable<T> callable)
Callable on the JavaFX Application Thread at some
unspecified time in the future and returns a Future that is set
on finish or error.
You need to evaluate the returned Future via (Future.get())
for exceptions or call the checkException() method to handle
exceptions after the task has finished.
T - the Callable typecallable - the CallableFuture result of the Callablepublic static <T> T waitFor(java.util.concurrent.Future<T> future)
Future to be set and then returns the
future result of type T.T - the type of the Futurefuture - the Future to wait for to be setFuturepublic static <T> T waitFor(long timeout,
java.util.concurrent.TimeUnit timeUnit,
java.util.concurrent.Future<T> future)
throws java.util.concurrent.TimeoutException
Future to be set and returns T, otherwise times out
with a TimeoutException.T - the type of the Futuretimeout - the timeout to wait fortimeUnit - the time unit timeout is infuture - the Future to wait for to be setFuturejava.util.concurrent.TimeoutException - if the wait timed outpublic static void waitFor(long timeout,
java.util.concurrent.TimeUnit timeUnit,
java.util.concurrent.Callable<java.lang.Boolean> condition)
throws java.util.concurrent.TimeoutException
Callable to return true otherwise times out with
a TimeoutException. The condition will be evaluated at least once. This method
will wait for the last condition to finish after a timeout.timeout - the timeout to wait fortimeUnit - the time unit timeout is incondition - the condition to wait for to be truejava.util.concurrent.TimeoutException - if the wait timed outpublic static void waitFor(long timeout,
java.util.concurrent.TimeUnit timeUnit,
javafx.beans.value.ObservableBooleanValue booleanValue)
throws java.util.concurrent.TimeoutException
ObservableBooleanValue to return true otherwise
times out with a TimeoutException.timeout - the timeout to wait fortimeUnit - the time unit timeout is inbooleanValue - the observablejava.util.concurrent.TimeoutException - if the wait timed outpublic static void waitForFxEvents()
public static void waitForFxEvents(int attemptsCount)
attemptsCount attempts for the event queue of the
"JavaFX Application Thread" to be completed, as well as any new events
triggered on it.attemptsCount - the number of attempts to trypublic static void sleep(long duration,
java.util.concurrent.TimeUnit timeUnit)
duration - the duration to sleeptimeUnit - the time unit duration is inpublic static void waitForAsync(long millis,
java.lang.Runnable runnable)
Runnable on a new Thread and waits millis
milliseconds for it to finish, otherwise times out with a TimeoutException.millis - number of milliseconds to waitrunnable - the Runnable to runpublic static <T> T waitForAsync(long millis,
java.util.concurrent.Callable<T> callable)
Callable on a new Thread and waits millis
milliseconds for it to finish. If finished, returns the future result of type
T, otherwise times out with a TimeoutException.T - the type returned by the Callablemillis - number of milliseconds to waitcallable - the Callable to callCallablepublic static void waitForAsyncFx(long millis,
java.lang.Runnable runnable)
Runnable on the JavaFX Application Thread at some unspecified time
in the future and waits millis milliseconds for it to finish, otherwise times out with
a TimeoutException.millis - number of milliseconds to waitrunnable - the Runnable to runpublic static <T> T waitForAsyncFx(long millis,
java.util.concurrent.Callable<T> callable)
Callable on the JavaFX Application Thread at some unspecified time
in the future and waits millis milliseconds for it to finish. If finished, returns
T otherwise times out with a TimeoutException.T - the type returned by the Callablemillis - number of milliseconds to waitcallable - the Callable to callCallablepublic static void checkException()
throws java.lang.Throwable
java.lang.Throwable - if an exception has occurred in an async taskpublic static void clearExceptions()