Top Description Fields Constructors Methods
org.apache.commons.lang3.concurrent

public Class CallableBackgroundInitializer<T>

extends BackgroundInitializer<T>
Class Inheritance
Type Parameters
<T>
the type of the object managed by this initializer class
Imports
java.util.Objects, java.util.concurrent.Callable, .ExecutorService

A specialized 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 java.util.concurrent.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 start() method. This causes the Callable to be executed in another thread. When the results of the Callable are needed the initializer's 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

Field Summary

Modifier and TypeField and Description
private final Callable<T>
callable

The Callable to be executed.

Constructor Summary

AccessConstructor and Description
public
CallableBackgroundInitializer(final Callable<T>
the Callable (must not be null).
call
)

Creates a new instance of CallableBackgroundInitializer and sets the Callable to be executed in a background thread.

public
CallableBackgroundInitializer(final Callable<T>
the Callable (must not be null).
call
,
final ExecutorService
an external ExecutorService to be used for task execution.
exec
)

Creates a new instance of CallableBackgroundInitializer and initializes it with the Callable to be executed in a background thread and the ExecutorService for managing the background execution.

Method Summary

Modifier and TypeMethod and Description
private void
checkCallable(final Callable<T>
the object to check.
callable
)

Tests the passed in Callable and throws an exception if it is undefined.

protected Exception
getTypedException(final Exception e)

Overrides org.apache.commons.lang3.concurrent.BackgroundInitializer.getTypedException.

Gets an Exception with a type of E as defined by a concrete subclass of this class.

protected T

Returns:

the result of the initialization.
initialize
()

Overrides org.apache.commons.lang3.concurrent.AbstractConcurrentInitializer.initialize.

Performs initialization in a background thread.

Inherited from org.apache.commons.lang3.concurrent.BackgroundInitializer:
buildergetgetActiveExecutorgetExternalExecutorgetFuturegetTaskCountisInitializedisStartedsetExternalExecutorstart

Field Detail

callableback to summary
private final Callable<T> callable

The Callable to be executed.

Constructor Detail

CallableBackgroundInitializerback to summary
public CallableBackgroundInitializer(final Callable<T> call)

Creates a new instance of CallableBackgroundInitializer and sets the Callable to be executed in a background thread.

Parameters
call:Callable<T>

the Callable (must not be null).

Exceptions
IllegalArgumentException:
if the Callable is null.
CallableBackgroundInitializerback to summary
public CallableBackgroundInitializer(final Callable<T> call, final ExecutorService exec)

Creates a new instance of CallableBackgroundInitializer and initializes it with the Callable to be executed in a background thread and the ExecutorService for managing the background execution.

Parameters
call:Callable<T>

the Callable (must not be null).

exec:ExecutorService

an external ExecutorService to be used for task execution.

Exceptions
IllegalArgumentException:
if the Callable is null.

Method Detail

checkCallableback to summary
private void checkCallable(final Callable<T> callable)

Tests the passed in Callable and throws an exception if it is undefined.

Parameters
callable:Callable<T>

the object to check.

Exceptions
IllegalArgumentException:
if the Callable is null.
getTypedExceptionback to summary
protected Exception getTypedException(final Exception e)

Overrides org.apache.commons.lang3.concurrent.BackgroundInitializer.getTypedException.

Doc from org.apache.commons.lang3.concurrent.BackgroundInitializer.getTypedException.

Gets an Exception with a type of E as defined by a concrete subclass of this class.

Annotations
@Override
initializeback to summary
protected T initialize() throws Exception

Overrides org.apache.commons.lang3.concurrent.AbstractConcurrentInitializer.initialize.

Performs initialization in a background thread. This implementation delegates to the Callable passed at construction time of this object.

Returns:T

the result of the initialization.

Annotations
@Override
Exceptions
Exception:
if an error occurs.