public class Late<T>
extends java.lang.Object
Background loading will start when some of the following three methods is called:
This class provides a part of the interface ofFuture, but does not allow for cancelling.
The main reason is that cancelling is not perfectly well-defined here.
If you need to become informed that the loading has finished, forward this information from the
value loader callable, possibly using something like SwingUtilities.invokeLater(Runnable).
| Constructor and Description |
|---|
Late(java.util.concurrent.ThreadPoolExecutor executor,
java.util.concurrent.Callable<? extends T> valueLoader)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
dispose(java.lang.Runnable notYetLoadedHandler,
java.util.function.Consumer<T> afterLoadHandler,
java.util.function.Consumer<java.lang.Exception> errorHandler)
Dispose the underlying value.
|
T |
get()
Get the value.
|
T |
get(long timeout,
java.util.concurrent.TimeUnit unit)
Get the value, waiting for a given amount of time.
|
java.util.concurrent.ThreadPoolExecutor |
getExecutor()
Get the thread pool executor on which this task operates.
|
T |
getOrDefault(T defaultValue)
Get the already retrieved value, or a default if loading has not yet finished
or has failed with an exception.
|
boolean |
isDone()
Is this task completed, either by retrieving the value or failing with an exception?
|
void |
triggerLoad()
Start loading the value if this has not already happened.
|
@NotNull public java.util.concurrent.ThreadPoolExecutor getExecutor()
public boolean isDone()
true: this task is finishedfalse: this task has either never started or is not yet finishedpublic T getOrDefault(T defaultValue)
defaultValue - default valuepublic T get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
java.lang.InterruptedException - if the retrieving task was interruptedjava.util.concurrent.ExecutionException - if the retrieving task failed with an exceptionpublic T get(long timeout, @NotNull java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
timeout - waiting duration before this method ends in a TimeoutExceptionunit - time unit of the timeoutjava.lang.InterruptedException - if the retrieving task was interruptedjava.util.concurrent.ExecutionException - if the retrieving task failed with an exceptionjava.util.concurrent.TimeoutException - if retrieving took more time than allowed by the parameterspublic void dispose(@NotNull java.lang.Runnable notYetLoadedHandler, @NotNull java.util.function.Consumer<T> afterLoadHandler, @NotNull java.util.function.Consumer<java.lang.Exception> errorHandler)
notYetLoadedHandler - called when the value was not yet loadedafterLoadHandler - called when the value was successfully loadederrorHandler - called when the loading of the value ran into an errorpublic void triggerLoad()
get()
from a single thread, which would effectively sequentialize the loading.
Calling this method for all values together upfront will load in parallel.