T- the type of the object managed by this initializer class
public abstract class BackgroundInitializer<T> extends Object implements ConcurrentInitializer<T>
A class that allows complex initialization operations in a background task.
Applications often have to do some expensive initialization steps when they are started, e.g. constructing a connection to a database, reading a configuration file, etc. Doing these things in parallel can enhance performance as the CPU load can be improved. However, when access to the resources initialized in a background thread is actually required, synchronization has to be performed to ensure that their initialization is complete.
This abstract base class provides support for this use case. A concrete
subclass must implement the
initialize() method. Here an arbitrary
initialization can be implemented, and a result object can be returned. With
this method in place the basic usage of this class is as follows (where
MyBackgroundInitializer is a concrete subclass):
MyBackgroundInitializer initializer = new MyBackgroundInitializer(); initializer.start(); // Now do some other things. Initialization runs in a parallel thread ... // Wait for the end of initialization and access the result object Object result = initializer.get();
After the construction of a
BackgroundInitializer object its
start() method has to be called. This starts the background
processing. The application can now continue to do other things. When it
needs access to the object produced by the
get() method. If initialization is already complete,
get() returns the result object immediately. Otherwise it blocks
until the result object is fully constructed.
BackgroundInitializer is a thin wrapper around a
object and uses an
ExecutorService for running the background
initialization task. It is possible to pass in an
construction time or set one using
start() was called. Then this object is used to spawn the background
task. If no
ExecutorService has been provided,
BackgroundInitializer creates a temporary
destroys it when initialization is complete.
The methods provided by
BackgroundInitializer provide for minimal
interaction with the wrapped
Future object. It is also possible to
Future object directly. Then the enhanced functionality
Future can be used, e.g. to check whether the background
operation is complete or to cancel the operation.
|Modifier||Constructor and Description|
Creates a new instance of
Creates a new instance of
|Modifier and Type||Method and Description|
Returns the result of the background initialization.
Returns the external
Returns the number of background tasks to be created for this initializer.
Performs the initialization.
Returns a flag whether this
Starts the background initialization.
BackgroundInitializer. No external
protected BackgroundInitializer(ExecutorService exec)
BackgroundInitializerand initializes it with the given
ExecutorService. If the
ExecutorServiceis not null, the background task for initializing this object will be scheduled at this service. Otherwise a new temporary
exec- an external
ExecutorServiceto be used for task execution
public final ExecutorService getExternalExecutor()
ExecutorServiceto be used by this class.
public boolean isStarted()
BackgroundInitializerhas already been started.
start()method has already been called
public final void setExternalExecutor(ExecutorService externalExecutor)
ExecutorServiceto be used by this class. The
ExecutorServicepassed to this method is used for executing the background task. Thus it is possible to re-use an already existing
ExecutorServiceor to use a specially configured one. If no
ExecutorServiceis set, this instance creates a temporary one and destroys it after background initialization is complete. Note that this method must be called before
start(); otherwise an exception is thrown.
ExecutorServiceto be used
IllegalStateException- if this initializer has already been started
public boolean start()
initialize()method in a background task. A
BackgroundInitializercan be started exactly once. The return value of this method determines whether the start was successful: only the first invocation of this method returns true, following invocations will return false.
public T get() throws ConcurrentException
InterruptedExceptionare wrapped in a
ConcurrentException. Calling this method before
start()was called causes an
IllegalStateExceptionexception to be thrown.
Futureobject that was created when
start()was called. Therefore this method can only be called after
protected final ExecutorService getActiveExecutor()
ExecutorServicethat is actually used for executing the background task. This method can be called after
start()it returns null). If an external executor was set, this is also the active executor. Otherwise this method returns the temporary executor that was created by this object.
ExecutorServicefor executing the background task
protected int getTaskCount()
ExecutorServiceis created. This base implementation returns 1. Derived classes that do more complex background processing can override it. This method is called from a synchronized block by the
start()method. Therefore overriding methods should be careful with obtaining other locks and return as fast as possible.
protected abstract T initialize() throws Exception
BackgroundInitializeris started. It must be implemented by a concrete subclass. An implementation is free to perform arbitrary initialization. The object returned by this method can be queried using the
Exception- if an error occurs
Copyright © 2001–2017 The Apache Software Foundation. All rights reserved.