Class MultiBackgroundInitializer

All Implemented Interfaces:
ConcurrentInitializer<MultiBackgroundInitializer.MultiBackgroundInitializerResults>, FailableSupplier<MultiBackgroundInitializer.MultiBackgroundInitializerResults,ConcurrentException>

A specialized 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:

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 BackgroundInitializer.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.

Since:
3.0