Class MultiBackgroundInitializer
- java.lang.Object
-
- org.apache.commons.lang3.concurrent.BackgroundInitializer<MultiBackgroundInitializer.MultiBackgroundInitializerResults>
-
- org.apache.commons.lang3.concurrent.MultiBackgroundInitializer
-
- All Implemented Interfaces:
ConcurrentInitializer<MultiBackgroundInitializer.MultiBackgroundInitializerResults>
public class MultiBackgroundInitializer extends BackgroundInitializer<MultiBackgroundInitializer.MultiBackgroundInitializerResults>
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 theaddInitializer(String, BackgroundInitializer)method. - After all initializers have been added, call the
BackgroundInitializer.start()method. - When access to the result objects produced by the
BackgroundInitializerobjects is needed call theBackgroundInitializer.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 theBackgroundInitializer.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMultiBackgroundInitializer.MultiBackgroundInitializerResultsA data class for storing the results of the background initialization performed byMultiBackgroundInitializer.
-
Constructor Summary
Constructors Constructor Description MultiBackgroundInitializer()Creates a new instance ofMultiBackgroundInitializer.MultiBackgroundInitializer(java.util.concurrent.ExecutorService exec)Creates a new instance ofMultiBackgroundInitializerand initializes it with the given externalExecutorService.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddInitializer(java.lang.String name, BackgroundInitializer<?> backgroundInitializer)Adds a newBackgroundInitializerto this object.protected intgetTaskCount()Returns the number of tasks needed for executing all childBackgroundInitializerobjects in parallel.protected MultiBackgroundInitializer.MultiBackgroundInitializerResultsinitialize()Creates the results object.-
Methods inherited from class org.apache.commons.lang3.concurrent.BackgroundInitializer
get, getActiveExecutor, getExternalExecutor, getFuture, isStarted, setExternalExecutor, start
-
-
-
-
Constructor Detail
-
MultiBackgroundInitializer
public MultiBackgroundInitializer()
Creates a new instance ofMultiBackgroundInitializer.
-
MultiBackgroundInitializer
public MultiBackgroundInitializer(java.util.concurrent.ExecutorService exec)
Creates a new instance ofMultiBackgroundInitializerand initializes it with the given externalExecutorService.- Parameters:
exec- theExecutorServicefor executing the background tasks
-
-
Method Detail
-
addInitializer
public void addInitializer(java.lang.String name, BackgroundInitializer<?> backgroundInitializer)Adds a newBackgroundInitializerto this object. When thisMultiBackgroundInitializeris started, the given initializer will be processed. This method must not be called afterBackgroundInitializer.start()has been invoked.- Parameters:
name- the name of the initializer (must not be null)backgroundInitializer- theBackgroundInitializerto add (must not be null)- Throws:
java.lang.IllegalArgumentException- if a required parameter is missingjava.lang.IllegalStateException- ifstart()has already been called
-
getTaskCount
protected int getTaskCount()
Returns the number of tasks needed for executing all childBackgroundInitializerobjects 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.- Overrides:
getTaskCountin classBackgroundInitializer<MultiBackgroundInitializer.MultiBackgroundInitializerResults>- Returns:
- the number of tasks required for background processing
-
initialize
protected MultiBackgroundInitializer.MultiBackgroundInitializerResults initialize() throws java.lang.Exception
Creates the results object. This implementation starts all childBackgroundInitializerobjects. 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.- Specified by:
initializein classBackgroundInitializer<MultiBackgroundInitializer.MultiBackgroundInitializerResults>- Returns:
- the results object
- Throws:
java.lang.Exception- if an error occurs
-
-