Applications often have to do some expensive initialization steps when they are started, e.g. constructing a connection to a database, reading a configuration file, etc. Doing these things in parallel can enhance performance as the CPU load can be improved. However, when access to the resources initialized in a background thread is actually required, synchronization has to be performed to ensure that their initialization is complete.
This abstract base class provides support for this use case. A concrete
subclass must implement the initialize() method. Here an arbitrary
initialization can be implemented, and a result object can be returned. With
this method in place the basic usage of this class is as follows (where
MyBackgroundInitializer is a concrete subclass):
MyBackgroundInitializer initializer = new MyBackgroundInitializer(); initializer.start(); // Now do some other things. Initialization runs in a parallel thread ... // Wait for the end of initialization and access the result object Object result = initializer.get();
After the construction of a BackgroundInitializer object its
start() method has to be called. This starts the background
processing. The application can now continue to do other things. When it
needs access to the object produced by the BackgroundInitializer it
calls its get() method. If initialization is already complete,
get() returns the result object immediately. Otherwise it blocks
until the result object is fully constructed.
BackgroundInitializer is a thin wrapper around a Future
object and uses an ExecutorService for running the background
initialization task. It is possible to pass in an ExecutorService at
construction time or set one using setExternalExecutor() before
start() was called. Then this object is used to spawn the background
task. If no ExecutorService has been provided, BackgroundInitializer creates a temporary ExecutorService and
destroys it when initialization is complete.
The methods provided by BackgroundInitializer provide for minimal
interaction with the wrapped Future object. It is also possible to
obtain the Future object directly. Then the enhanced functionality
offered by Future can be used, e.g. to check whether the background
operation is complete or to cancel the operation.
| Modifier and Type | Class and Description |
|---|---|
| public static class | BackgroundInitializer.
The type of the initializer managed by this builder. I extends BackgroundInitializerThe type of results supplied by this builder. T>Builds a new instance. |
| private class |
| Modifier and Type | Field and Description |
|---|---|
| private ExecutorService | executor
A reference to the executor service that is actually used. |
| private ExecutorService | externalExecutor
The external executor service for executing tasks. |
| private Future | future
Stores the handle to the background task. |
| Access | Constructor and Description |
|---|---|
| protected | |
| protected | BackgroundInitializer(final ExecutorService
an external exec)ExecutorService to be used for task.
executionCreates a new instance of |
| private | BackgroundInitializer(final FailableSupplier<T, ConcurrentException>
the initializer supplier called by initializer, final FailableConsumer<T, ConcurrentException> initialize().the closer consumer called by closer, final ExecutorService close().the exec)ExecutorService to be used @see #setExternalExecutor(ExecutorService)Constructs a new instance. |
| Modifier and Type | Method and Description |
|---|---|
| public static < the type of object to build. T> BackgroundInitializer. | |
| private ExecutorService | |
| private Callable | Returns: a task for the background initialization.the execDestroy)ExecutorService to be destroyed by the
task.Creates a task for the background initialization. |
| public T | Returns: the object produced by this initializer.Implements org. Gets the result of the background initialization. |
| protected final synchronized ExecutorService | Returns: theExecutorService for executing the background task.Gets the |
| public final synchronized ExecutorService | Returns: theExecutorService.Gets the external |
| public synchronized Future | |
| protected int | Returns: the number of background tasks required by this initializer.Gets the number of background tasks to be created for this initializer. |
| protected Exception | getTypedException(final Exception e)
Implements abstract org. Gets an Exception with a type of E as defined by a concrete subclass of this class. |
| public synchronized boolean | Returns: true if initialization completed successfully, otherwise false.Implements abstract org. Tests whether this instance is initialized. |
| public synchronized boolean | Returns: a flag whether thestart() method has already been
called.Tests whether this |
| public final synchronized void | setExternalExecutor(final ExecutorService
the externalExecutor)ExecutorService to be used.Sets an |
| public synchronized boolean | Returns: a flag whether the initializer could be started successfully.Starts the background initialization. |
| executor | back to summary |
|---|---|
| private ExecutorService executor A reference to the executor service that is actually used. | |
| externalExecutor | back to summary |
|---|---|
| private ExecutorService externalExecutor The external executor service for executing tasks. | |
| future | back to summary |
|---|---|
| private Future<T> future Stores the handle to the background task. | |
| BackgroundInitializer | back to summary |
|---|---|
| protected BackgroundInitializer() Creates a new instance of | |
| BackgroundInitializer | back to summary |
|---|---|
| protected BackgroundInitializer(final ExecutorService exec) Creates a new instance of
| |
| BackgroundInitializer | back to summary |
|---|---|
| private BackgroundInitializer(final FailableSupplier<T, ConcurrentException> initializer, final FailableConsumer<T, ConcurrentException> closer, final ExecutorService exec) Constructs a new instance.
| |
| builder | back to summary |
|---|---|
| public static <T> BackgroundInitializer. Creates a new builder.
| |
| createExecutor | back to summary |
|---|---|
| private ExecutorService createExecutor() Creates the
| |
| createTask | back to summary |
|---|---|
| private Callable Creates a task for the background initialization. The
| |
| get | back to summary |
|---|---|
| public T get() throws ConcurrentException Implements org. Gets the result of the background initialization. This method blocks
until initialization is complete. If the background processing caused a
runtime exception, it is directly thrown by this method. Checked
exceptions, including
| |
| getActiveExecutor | back to summary |
|---|---|
| protected final synchronized ExecutorService getActiveExecutor() Gets the
| |
| getExternalExecutor | back to summary |
|---|---|
| public final synchronized ExecutorService getExternalExecutor() Gets the external
| |
| getFuture | back to summary |
|---|---|
| public synchronized Future Gets the
| |
| getTaskCount | back to summary |
|---|---|
| protected int getTaskCount() Gets the number of background tasks to be created for this
initializer. This information is evaluated when a temporary
| |
| getTypedException | back to summary |
|---|---|
| protected Exception getTypedException(final Exception e) Implements abstract org. Doc from org. Gets an Exception with a type of E as defined by a concrete subclass of this class.
| |
| isInitialized | back to summary |
|---|---|
| public synchronized boolean isInitialized() Implements abstract org. Tests whether this instance is initialized. Once initialized, always returns true. If initialization failed then the failure will be cached and this will never return true.
| |
| isStarted | back to summary |
|---|---|
| public synchronized boolean isStarted() Tests whether this
| |
| setExternalExecutor | back to summary |
|---|---|
| public final synchronized void setExternalExecutor(final ExecutorService externalExecutor) Sets an
| |
| start | back to summary |
|---|---|
| public synchronized boolean start() Starts the background initialization. With this method the initializer
becomes active and invokes the
| |
| Modifier and Type | Field and Description |
|---|---|
| private ExecutorService | externalExecutor
The external executor service for executing tasks. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public I | |
| public BackgroundInitializer. | Returns: this instance.the externalExecutor)ExecutorService to be used.Sets the external executor service for executing tasks. |
| externalExecutor | back to summary |
|---|---|
| private ExecutorService externalExecutor The external executor service for executing tasks. null is a permitted value. | |
| Builder | back to summary |
|---|---|
| public Builder() Constructs a new instance. | |
| get | back to summary |
|---|---|
| public I get() Implements org. Doc from org. Supplies an object
| |
| setExternalExecutor | back to summary |
|---|---|
| public BackgroundInitializer. Sets the external executor service for executing tasks. null is a permitted value.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final ExecutorService | execFinally
Stores the executor service to be destroyed at the end. |
| Access | Constructor and Description |
|---|---|
| pack-priv | InitializationTask(final ExecutorService
the exec)ExecutorServiceCreates a new instance of |
| Modifier and Type | Method and Description |
|---|---|
| public T | Returns: the result objectImplements java. Initiates initialization and returns the result. |
| execFinally | back to summary |
|---|---|
| private final ExecutorService execFinally Stores the executor service to be destroyed at the end. | |
| InitializationTask | back to summary |
|---|---|
| pack-priv InitializationTask(final ExecutorService exec) Creates a new instance of
| |
| call | back to summary |
|---|---|
| public T call() throws Exception Implements java. Initiates initialization and returns the result. | |