BackgroundInitializer implementation that wraps a
Callable object.
An instance of this class is initialized with a Callable object when
it is constructed. The implementation of the initialize() method
defined in the super class delegates to this Callable so that the
Callable is executed in the background thread.
The java. interface is a standard mechanism
of the JDK to define tasks to be executed by another thread. The CallableBackgroundInitializer class allows combining this standard interface
with the background initializer API.
Usage of this class is very similar to the default usage pattern of the
BackgroundInitializer class: Just create an instance and provide the
Callable object to be executed, then call the initializer's
start() method. This causes the Callable to be executed in
another thread. When the results of the Callable are needed the
initializer's get() method can be called (which may block until
background execution is complete). The following code fragment shows a
typical usage example:
// a Callable that performs a complex computation
Callable<Integer> computationCallable = new MyComputationCallable();
// setup the background initializer
CallableBackgroundInitializer<Integer> initializer =
new CallableBackgroundInitializer(computationCallable);
initializer.start();
// Now do some other things. Initialization runs in a parallel thread
...
// Wait for the end of initialization and access the result
Integer result = initializer.get();
| Modifier and Type | Field and Description |
|---|---|
| private final Callable | callable
The Callable to be executed. |
| Access | Constructor and Description |
|---|---|
| public | CallableBackgroundInitializer(final Callable<T>
the call)Callable (must not be null).Creates a new instance of |
| public | CallableBackgroundInitializer(final Callable<T>
the call, final ExecutorService Callable (must not be null).an external exec)ExecutorService to be used for task execution.Creates a new instance of |
| Modifier and Type | Method and Description |
|---|---|
| private void | checkCallable(final Callable<T>
the object to check. callable)Tests the passed in |
| protected Exception | getTypedException(final Exception e)
Overrides org. Gets an Exception with a type of E as defined by a concrete subclass of this class. |
| protected T | Returns: the result of the initialization.Overrides org. Performs initialization in a background thread. |
| callable | back to summary |
|---|---|
| private final Callable<T> callable The Callable to be executed. | |
| CallableBackgroundInitializer | back to summary |
|---|---|
| public CallableBackgroundInitializer(final Callable<T> call) Creates a new instance of
| |
| CallableBackgroundInitializer | back to summary |
|---|---|
| public CallableBackgroundInitializer(final Callable<T> call, final ExecutorService exec) Creates a new instance of
| |
| checkCallable | back to summary |
|---|---|
| private void checkCallable(final Callable<T> callable) Tests the passed in
| |
| getTypedException | back to summary |
|---|---|
| protected Exception getTypedException(final Exception e) Overrides org. Doc from org. Gets an Exception with a type of E as defined by a concrete subclass of this class.
| |
| initialize | back to summary |
|---|---|
| protected T initialize() throws Exception Overrides org. Performs initialization in a background thread. This implementation delegates to the | |