Class CallableBackgroundInitializer<T>
- Type Parameters:
T- The type of the object managed by this initializer class
- All Implemented Interfaces:
ConcurrentInitializer<T>, FailableSupplier<T, ConcurrentException>
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 Callable 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
BackgroundInitializer.start() method. This causes the Callable to be executed in
another thread. When the results of the Callable are needed the
initializer's BackgroundInitializer.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();
- Since:
- 3.0
-
Nested Class Summary
Nested classes/interfaces inherited from class BackgroundInitializer
BackgroundInitializer.Builder<I,T> Modifier and TypeClassDescriptionstatic classBackgroundInitializer.Builder<I extends BackgroundInitializer<T>, T>Builds a new instance.Nested classes/interfaces inherited from class AbstractConcurrentInitializer
AbstractConcurrentInitializer.AbstractBuilder<I,T, B, E> Modifier and TypeClassDescriptionstatic classAbstractConcurrentInitializer.AbstractBuilder<I extends AbstractConcurrentInitializer<T,E>, T, B extends AbstractConcurrentInitializer.AbstractBuilder<I, T, B, E>, E extends Exception> Builds a new instance for subclasses. -
Field Summary
Fields inherited from interface FailableSupplier
NULModifier and TypeFieldDescriptionstatic final FailableSupplierReturns the singleton supplier that always returns null. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance ofCallableBackgroundInitializerand sets theCallableto be executed in a background thread.CallableBackgroundInitializer(Callable<T> call, ExecutorService exec) Creates a new instance ofCallableBackgroundInitializerand initializes it with theCallableto be executed in a background thread and theExecutorServicefor managing the background execution. -
Method Summary
Methods inherited from class BackgroundInitializer
builder, get, getActiveExecutor, getExternalExecutor, getFuture, getTaskCount, isInitialized, isStarted, setExternalExecutor, startModifier and TypeMethodDescriptionstatic <T> BackgroundInitializer.Builder<BackgroundInitializer<T>, T> builder()Creates a new builder.get()Gets the result of the background initialization.protected final ExecutorServiceGets theExecutorServicethat is actually used for executing the background task.final ExecutorServiceGets the externalExecutorServiceto be used by this class.Gets theFutureobject that was created whenBackgroundInitializer.start()was called.protected intGets the number of background tasks to be created for this initializer.booleanTests whether this instance is initialized.booleanTests whether thisBackgroundInitializerhas already been started.final voidsetExternalExecutor(ExecutorService externalExecutor) Sets anExecutorServiceto be used by this class.booleanstart()Starts the background initialization.Methods inherited from class AbstractConcurrentInitializer
close
-
Constructor Details
-
CallableBackgroundInitializer
Creates a new instance ofCallableBackgroundInitializerand sets theCallableto be executed in a background thread.- Parameters:
call- TheCallable(must not benull).- Throws:
IllegalArgumentException- Thrown if theCallableisnull.
-
CallableBackgroundInitializer
Creates a new instance ofCallableBackgroundInitializerand initializes it with theCallableto be executed in a background thread and theExecutorServicefor managing the background execution.- Parameters:
call- TheCallable(must not benull).exec- An externalExecutorServiceto be used for task execution.- Throws:
IllegalArgumentException- Thrown if theCallableisnull.
-
-
Method Details
-
getTypedException
Gets an Exception with a type of E as defined by a concrete subclass of this class.- Overrides:
getTypedExceptionin classBackgroundInitializer<T>- Parameters:
e- The actual exception that was thrown.- Returns:
- A new exception with the actual type of E, that wraps e.
-
initialize
Performs initialization in a background thread. This implementation delegates to theCallablepassed at construction time of this object.- Overrides:
initializein classAbstractConcurrentInitializer<T, Exception>- Returns:
- The result of the initialization.
- Throws:
Exception- Thrown if an error occurs.
-