MultiBackgroundInitializer¶
- public class org.apache.commons.lang3.concurrent.MultiBackgroundInitializer extends BackgroundInitializer<MultiBackgroundInitializer.MultiBackgroundInitializerResults>[source]¶
A specialized
BackgroundInitializerimplementation 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 ofBackgroundInitializerobjects, 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 anExecutorServiceand shares it with theBackgroundInitializerobjects it is responsible for; so the using application need not bother with these details.The typical usage scenario for
MultiBackgroundInitializeris as follows:Create a new instance of the class. Optionally pass in a pre-configured
ExecutorService. AlternativelyMultiBackgroundInitializercan create a temporaryExecutorServiceand delete it after initialization is complete.Create specialized
BackgroundInitializerobjects for the initialization tasks to be performed and add them to theMultiBackgroundInitializerusing the#addInitializer(String, BackgroundInitializer)method.After all initializers have been added, call the
#start()method.When access to the result objects produced by the
BackgroundInitializerobjects 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.
MultiBackgroundInitializerstarts a special controller task that starts allBackgroundInitializerobjects added to the instance. Before the an initializer is started it is checked whether this initializer already has anExecutorServiceset. If this is the case, thisExecutorServiceis used for running the background task. Otherwise the currentExecutorServiceof thisMultiBackgroundInitializeris shared with the initializer.The easiest way of using this class is to let it deal with the management of an
ExecutorServiceitself: If no externalExecutorServiceis provided, the class creates a temporaryExecutorService(that is capable of executing all background tasks in parallel) and destroys it at the end of background processing.Alternatively an external
ExecutorServicecan be provided - either at construction time or later by calling the#setExternalExecutor(ExecutorService)method. In this case all background tasks are scheduled at this externalExecutorService. Important note: When using an externalExecutorServicebe 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:MultiBackgroundInitializerstarts a task that starts all registeredBackgroundInitializerobjects and waits for their completion. If for instance a single threadedExecutorServiceis used, none of the background tasks can be executed, and the task created byMultiBackgroundInitializerwaits forever.- Since:
3.0
Constructors¶
Constructs a new instance of |
|
Constructs a new instance of |
- public MultiBackgroundInitializer()[source]¶
Constructs a new instance of
MultiBackgroundInitializer.
- public MultiBackgroundInitializer(ExecutorService exec)[source]¶
Constructs a new instance of
MultiBackgroundInitializerand initializes it with the given externalExecutorService.- Parameters:
exec – The
ExecutorServicefor executing the background tasks.
Methods¶
Adds a new |
|
Calls the closer of all child |
|
Gets the number of tasks needed for executing all child |
|
Creates the results object. |
|
Tests whether this all child |
- public void addInitializer(String name, BackgroundInitializer<?> backgroundInitializer)[source]¶
Adds a new
BackgroundInitializerto this object. When thisMultiBackgroundInitializeris started, the given initializer will be processed. This method must not be called after#start()has been invoked.- Parameters:
name – The name of the initializer (must not be
null).backgroundInitializer – The
BackgroundInitializerto add (must not benull).
- Throws:
java.lang.NullPointerException – Thrown if either
nameorbackgroundInitializerisnull.java.lang.IllegalStateException – Thrown if
start()has already been called.
- @Override public void close() throws ConcurrentException[source]¶
Calls the closer of all child
BackgroundInitializerobjects.- Throws:
org.apache.commons.lang3.concurrent.ConcurrentException – Thrown with all other exceptions as suppressed exceptions. ConcurrentException instances thrown by children are unwrapped.
- Since:
3.14.0
- @Override protected int getTaskCount()[source]¶
Gets the number of tasks needed for executing all child
BackgroundInitializerobjects in parallel. This implementation sums up the required tasks for all child initializers (which is necessary if one of the child initializers is itself aMultiBackgroundInitializer). Then it adds 1 for the control task that waits for the completion of the children.- Returns:
The number of tasks required for background processing.
- @Override protected MultiBackgroundInitializer.MultiBackgroundInitializerResults initialize() throws Exception[source]¶
Creates the results object. This implementation starts all child
BackgroundInitializerobjects. Then it collects their results and creates aMultiBackgroundInitializerResultsobject with this data. If a child initializer throws a checked exceptions, it is added to the results object. Unchecked exceptions are propagated.- Returns:
The results object.
- Throws:
java.lang.Exception – Thrown if an error occurs.
- @Override public boolean isInitialized()[source]¶
Tests whether this all child
BackgroundInitializerobjects are initialized. Once initialized, always returns true.- Returns:
Whether all child
BackgroundInitializerobjects instance are initialized. Once initialized, always returns true. If there are no childBackgroundInitializerobjects return false.- Since:
3.14.0