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
-
Methods inherited from class org.apache.commons.lang3.concurrent.BackgroundInitializer
get, getExternalExecutor, getFuture, 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
-
-