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
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
java.util.concurrent.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();- Since:
- 3.0
-
-
Constructor Summary
Constructors Constructor Description CallableBackgroundInitializer(java.util.concurrent.Callable<T> call)Creates a new instance ofCallableBackgroundInitializerand sets theCallableto be executed in a background thread.CallableBackgroundInitializer(java.util.concurrent.Callable<T> call, java.util.concurrent.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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Tinitialize()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 ofCallableBackgroundInitializerand sets theCallableto be executed in a background thread.- Parameters:
call- theCallable(must not be null)- Throws:
java.lang.IllegalArgumentException- if theCallableis null
-
CallableBackgroundInitializer
public CallableBackgroundInitializer(java.util.concurrent.Callable<T> call, java.util.concurrent.ExecutorService exec)
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 be null)exec- an externalExecutorServiceto be used for task execution- Throws:
java.lang.IllegalArgumentException- if theCallableis null
-
-
Method Detail
-
initialize
protected T initialize() throws java.lang.Exception
Performs initialization in a background thread. This implementation delegates to theCallablepassed at construction time of this object.- Specified by:
initializein classBackgroundInitializer<T>- Returns:
- the result of the initialization
- Throws:
java.lang.Exception- if an error occurs
-
-