Class CallableBackgroundInitializer<T>
- java.lang.Object
-
- org.apache.commons.lang3.concurrent.BackgroundInitializer<T>
-
- org.apache.commons.lang3.concurrent.CallableBackgroundInitializer<T>
-
- Type Parameters:
T
- the type of the object managed by this initializer class
- All Implemented Interfaces:
ConcurrentInitializer<T>
public class CallableBackgroundInitializer<T> extends BackgroundInitializer<T>
A specialized
BackgroundInitializer
implementation that wraps aCallable
object.An instance of this class is initialized with a
Callable
object when it is constructed. The implementation of theinitialize()
method defined in the super class delegates to thisCallable
so that theCallable
is executed in the background thread.The
java.util.concurrent.Callable
interface is a standard mechanism of the JDK to define tasks to be executed by another thread. TheCallableBackgroundInitializer
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 theCallable
object to be executed, then call the initializer'sBackgroundInitializer.start()
method. This causes theCallable
to be executed in another thread. When the results of theCallable
are 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();
- Since:
- 3.0
-
-
Constructor Summary
Constructors Constructor Description CallableBackgroundInitializer(java.util.concurrent.Callable<T> call)
Creates a new instance ofCallableBackgroundInitializer
and sets theCallable
to be executed in a background thread.CallableBackgroundInitializer(java.util.concurrent.Callable<T> call, java.util.concurrent.ExecutorService exec)
Creates a new instance ofCallableBackgroundInitializer
and initializes it with theCallable
to be executed in a background thread and theExecutorService
for managing the background execution.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected T
initialize()
Performs initialization in a background thread.-
Methods inherited from class org.apache.commons.lang3.concurrent.BackgroundInitializer
get, getActiveExecutor, getExternalExecutor, getFuture, getTaskCount, isStarted, setExternalExecutor, start
-
-
-
-
Constructor Detail
-
CallableBackgroundInitializer
public CallableBackgroundInitializer(java.util.concurrent.Callable<T> call)
Creates a new instance ofCallableBackgroundInitializer
and sets theCallable
to be executed in a background thread.- Parameters:
call
- theCallable
(must not be null)- Throws:
java.lang.IllegalArgumentException
- if theCallable
is null
-
CallableBackgroundInitializer
public CallableBackgroundInitializer(java.util.concurrent.Callable<T> call, java.util.concurrent.ExecutorService exec)
Creates a new instance ofCallableBackgroundInitializer
and initializes it with theCallable
to be executed in a background thread and theExecutorService
for managing the background execution.- Parameters:
call
- theCallable
(must not be null)exec
- an externalExecutorService
to be used for task execution- Throws:
java.lang.IllegalArgumentException
- if theCallable
is null
-
-
Method Detail
-
initialize
protected T initialize() throws java.lang.Exception
Performs initialization in a background thread. This implementation delegates to theCallable
passed at construction time of this object.- Specified by:
initialize
in classBackgroundInitializer<T>
- Returns:
- the result of the initialization
- Throws:
java.lang.Exception
- if an error occurs
-
-