public class MultiBackgroundInitializer extends BackgroundInitializer<MultiBackgroundInitializer.MultiBackgroundInitializerResults>
BackgroundInitializer implementation that can deal with
multiple background initialization tasks.
This class has a similar purpose as
it is not limited to a single background initialization task. Rather it
manages an arbitrary number of
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
BackgroundInitializer objects it is responsible for; so the
using application need not bother with these details.
The typical usage scenario for
MultiBackgroundInitializer is as
MultiBackgroundInitializercan create a temporary
ExecutorServiceand delete it after initialization is complete.
BackgroundInitializerobjects for the initialization tasks to be performed and add them to the
BackgroundInitializerobjects is needed call the
BackgroundInitializer.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
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
ExecutorService of this
shared with the initializer.
The easiest way of using this class is to let it deal with the management of
ExecutorService itself: If no external
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
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
MultiBackgroundInitializer starts a task that
starts all registered
BackgroundInitializer objects and waits for
their completion. If for instance a single threaded
is used, none of the background tasks can be executed, and the task created
MultiBackgroundInitializer waits forever.
|Modifier and Type||Class and Description|
A data class for storing the results of the background initialization performed by
|Constructor and Description|
Creates a new instance of
Creates a new instance of
|Modifier and Type||Method and Description|
Adds a new
Returns the number of tasks needed for executing all child
Creates the results object.
get, getActiveExecutor, getExternalExecutor, getFuture, isStarted, setExternalExecutor, start
public void addInitializer(String name, BackgroundInitializer<?> init)
BackgroundInitializerto this object. When this
MultiBackgroundInitializeris started, the given initializer will be processed. This method must not be called after
BackgroundInitializer.start()has been invoked.
protected int getTaskCount()
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 a
MultiBackgroundInitializer). Then it adds 1 for the control task that waits for the completion of the children.
protected MultiBackgroundInitializer.MultiBackgroundInitializerResults initialize() throws Exception
BackgroundInitializerobjects. Then it collects their results and creates a
MultiBackgroundInitializerResultsobject with this data. If a child initializer throws a checked exceptions, it is added to the results object. Unchecked exceptions are propagated.
Exception- if an error occurs
Copyright © 2001–2016 The Apache Software Foundation. All rights reserved.