CallableBackgroundInitializer¶
- public class org.apache.commons.lang3.concurrent.CallableBackgroundInitializer<T> extends BackgroundInitializer<T>[source]¶
A specialized
BackgroundInitializerimplementation that wraps aCallableobject.An instance of this class is initialized with a
Callableobject when it is constructed. The implementation of theinitialize()method defined in the super class delegates to thisCallableso that theCallableis executed in the background thread.The
Callableinterface is a standard mechanism of the JDK to define tasks to be executed by another thread. TheCallableBackgroundInitializerclass allows combining this standard interface with the background initializer API.Usage of this class is very similar to the default usage pattern of the
BackgroundInitializerclass: Just create an instance and provide theCallableobject to be executed, then call the initializer'sBackgroundInitializer.start()method. This causes theCallableto be executed in another thread. When the results of theCallableare needed the initializer'sBackgroundInitializer.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();
- Type parameters:
<T> – The type of the object managed by this initializer class
- Since:
3.0
Constructors¶
Creates a new instance of |
|
|
Creates a new instance of |
- public CallableBackgroundInitializer(Callable<T> call)[source]¶
Creates a new instance of
CallableBackgroundInitializerand sets theCallableto be executed in a background thread.- Parameters:
call – The
Callable(must not benull).- Throws:
IllegalArgumentException– Thrown if theCallableisnull.
- public CallableBackgroundInitializer(Callable<T> call, ExecutorService exec)[source]¶
Creates a new instance of
CallableBackgroundInitializerand initializes it with theCallableto be executed in a background thread and theExecutorServicefor managing the background execution.- Parameters:
call – The
Callable(must not benull).exec – An external
ExecutorServiceto be used for task execution.
- Throws:
IllegalArgumentException– Thrown if theCallableisnull.
Methods¶
|
Gets an Exception with a type of E as defined by a concrete subclass of this class. |
|
|
Performs initialization in a background thread. |
- protected Exception getTypedException(Exception e)[source]¶
Gets an Exception with a type of E as defined by a concrete subclass of this class.
- Overrides:
getTypedExceptionin classBackgroundInitializer- Parameters:
e – The actual exception that was thrown.
- Returns:
A new exception with the actual type of E, that wraps e.
- protected T initialize() throws Exception[source]¶
Performs initialization in a background thread. This implementation delegates to the
Callablepassed at construction time of this object.- Overrides:
initializein classAbstractConcurrentInitializer- Returns:
The result of the initialization.
- Throws:
Exception– Thrown if an error occurs.