BackgroundInitializer implementation that can deal with
multiple background initialization tasks.
This class has a similar purpose as BackgroundInitializer. However,
it is not limited to a single background initialization task. Rather it
manages an arbitrary number of BackgroundInitializer objects,
executes them, and waits until they are completely initialized. This is
useful for applications that have to perform multiple initialization tasks
that can run in parallel (i.e. that do not depend on each other). This class
takes care about the management of an ExecutorService and shares it
with the BackgroundInitializer objects it is responsible for; so the
using application need not bother with these details.
The typical usage scenario for MultiBackgroundInitializer is as
follows:
ExecutorService. Alternatively MultiBackgroundInitializer can
create a temporary ExecutorService and delete it after initialization
is complete.BackgroundInitializer objects for the
initialization tasks to be performed and add them to the MultiBackgroundInitializer using the
addInitializer(String, BackgroundInitializer) method.start() method.
BackgroundInitializer objects is needed call the get() method. The
object returned here provides access to all result objects created during
initialization. It also stores information about exceptions that have
occurred.
MultiBackgroundInitializer starts a special controller task that
starts all BackgroundInitializer objects added to the instance.
Before the an initializer is started it is checked whether this initializer
already has an ExecutorService set. If this is the case, this ExecutorService is used for running the background task. Otherwise the
current ExecutorService of this MultiBackgroundInitializer is
shared with the initializer.
The easiest way of using this class is to let it deal with the management of
an ExecutorService itself: If no external ExecutorService is
provided, the class creates a temporary ExecutorService (that is
capable of executing all background tasks in parallel) and destroys it at the
end of background processing.
Alternatively an external ExecutorService can be provided - either at
construction time or later by calling the
setExternalExecutor(ExecutorService) method. In this case all
background tasks are scheduled at this external ExecutorService
Important Note
When using an external ExecutorService be sure that the number of threads managed by the service is
large enough. Otherwise a deadlock can happen! This is the case in the
following scenario: MultiBackgroundInitializer starts a task that
starts all registered BackgroundInitializer objects and waits for
their completion. If for instance a single threaded ExecutorService
is used, none of the background tasks can be executed, and the task created
by MultiBackgroundInitializer waits forever.
| Modifier and Type | Class and Description |
|---|---|
| public static class | MultiBackgroundInitializer.
A data class for storing the results of the background initialization
performed by |
| Modifier and Type | Field and Description |
|---|---|
| private final Map | childInitializers
A map with the child initializers. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | MultiBackgroundInitializer(final ExecutorService
the exec)ExecutorService for executing the background tasks.Constructs a new instance of |
| Modifier and Type | Method and Description |
|---|---|
| public void | addInitializer(final String
the name of the initializer (must not be null). name, final BackgroundInitializer<?> the backgroundInitializer)BackgroundInitializer to add (must not be null).Adds a new |
| public void | close()
Overrides org. Calls the closer of all child |
| protected int | Returns: the number of tasks required for background processing.Overrides org. Gets the number of tasks needed for executing all child |
| protected MultiBackgroundInitializer. | Returns: the results object.Overrides org. Creates the results object. |
| public boolean | Returns: Whether all childBackgroundInitializer objects instance are initialized. Once initialized, always returns true. If there are no child
BackgroundInitializer objects return false.Overrides org. Tests whether this all child |
| childInitializers | back to summary |
|---|---|
| private final Map<String, BackgroundInitializer<?>> childInitializers A map with the child initializers. | |
| MultiBackgroundInitializer | back to summary |
|---|---|
| public MultiBackgroundInitializer() Constructs a new instance of | |
| MultiBackgroundInitializer | back to summary |
|---|---|
| public MultiBackgroundInitializer(final ExecutorService exec) Constructs a new instance of
| |
| addInitializer | back to summary |
|---|---|
| public void addInitializer(final String name, final BackgroundInitializer<?> backgroundInitializer) Adds a new
| |
| close | back to summary |
|---|---|
| public void close() throws ConcurrentException Overrides org. Calls the closer of all child
| |
| getTaskCount | back to summary |
|---|---|
| protected int getTaskCount() Overrides org. Gets the number of tasks needed for executing all child
| |
| initialize | back to summary |
|---|---|
| protected MultiBackgroundInitializer. Overrides org. Creates the results object. This implementation starts all child
| |
| isInitialized | back to summary |
|---|---|
| public boolean isInitialized() Overrides org. Tests whether this all child
| |
MultiBackgroundInitializer. Objects of this inner
class are returned by MultiBackgroundInitializer#initialize().
They allow access to all result objects produced by the
BackgroundInitializer objects managed by the owning instance. It
is also possible to retrieve status information about single
BackgroundInitializers, i.e. whether they completed normally or
caused an exception.
| Modifier and Type | Field and Description |
|---|---|
| private final Map | exceptions
A map with the exceptions. |
| private final Map | initializers
A map with the child initializers. |
| private final Map | resultObjects
A map with the result objects. |
| Access | Constructor and Description |
|---|---|
| private | MultiBackgroundInitializerResults(final Map<String, BackgroundInitializer<?>>
the initializers, final Map<String, Object> BackgroundInitializer objects.the result objects. resultObjects, final Map<String, ConcurrentException> the exceptions. exceptions)Creates a new instance of |
| Modifier and Type | Method and Description |
|---|---|
| private BackgroundInitializer | |
| public ConcurrentException | Returns: the exception thrown by this initializer.the name of the name)BackgroundInitializer.Gets the |
| public BackgroundInitializer | Returns: theBackgroundInitializer with this name.the name of the name)BackgroundInitializer.Gets the |
| public Object | Returns: the result object produced by thisBackgroundInitializer.the name of the name)BackgroundInitializer.Gets the result object produced by the |
| public Set | Returns: an (unmodifiable) set with the names of the managedBackgroundInitializer objects.Returns a set with the names of all |
| public boolean | Returns: a flag whether this initializer caused an exception.the name of the name)BackgroundInitializer.Tests whether the |
| public boolean | Returns: a flag whether the initialization was successful.Tests whether the whole initialization was successful. |
| exceptions | back to summary |
|---|---|
| private final Map<String, ConcurrentException> exceptions A map with the exceptions. | |
| initializers | back to summary |
|---|---|
| private final Map<String, BackgroundInitializer<?>> initializers A map with the child initializers. | |
| resultObjects | back to summary |
|---|---|
| private final Map<String, Object> resultObjects A map with the result objects. | |
| MultiBackgroundInitializerResults | back to summary |
|---|---|
| private MultiBackgroundInitializerResults(final Map<String, BackgroundInitializer<?>> initializers, final Map<String, Object> resultObjects, final Map<String, ConcurrentException> exceptions) Creates a new instance of
| |
| checkName | back to summary |
|---|---|
| private BackgroundInitializer Checks whether an initializer with the given name exists. If not, throws an exception. If it exists, the associated child initializer is returned.
| |
| getException | back to summary |
|---|---|
| public ConcurrentException getException(final String name) Gets the
| |
| getInitializer | back to summary |
|---|---|
| public BackgroundInitializer Gets the
| |
| getResultObject | back to summary |
|---|---|
| public Object getResultObject(final String name) Gets the result object produced by the
| |
| initializerNames | back to summary |
|---|---|
| public Set Returns a set with the names of all | |
| isException | back to summary |
|---|---|
| public boolean isException(final String name) Tests whether the
| |
| isSuccessful | back to summary |
|---|---|
| public boolean isSuccessful() Tests whether the whole initialization was successful. This is the case if no child initializer has thrown an exception.
| |