BackgroundInitializer
- public class org.apache.commons.lang3.concurrent.BackgroundInitializer<T> extends AbstractConcurrentInitializer<T, Exception>[source]
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 (whereMyBackgroundInitializeris 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
BackgroundInitializerobject 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 theBackgroundInitializerit calls its#get()method. If initialization is already complete,#get()returns the result object immediately. Otherwise it blocks until the result object is fully constructed.BackgroundInitializeris a thin wrapper around aFutureobject and uses anExecutorServicefor running the background initialization task. It is possible to pass in anExecutorServiceat construction time or set one usingsetExternalExecutor()beforestart()was called. Then this object is used to spawn the background task. If noExecutorServicehas been provided,BackgroundInitializercreates a temporaryExecutorServiceand destroys it when initialization is complete.The methods provided by
BackgroundInitializerprovide for minimal interaction with the wrappedFutureobject. It is also possible to obtain theFutureobject directly. Then the enhanced functionality offered byFuturecan be used, e.g. to check whether the background operation is complete or to cancel the operation.- Parameters:
<T> – The type of the object managed by this initializer class
- Since:
3.0
Constructors
Creates a new instance of |
|
Creates a new instance of |
- protected BackgroundInitializer()[source]
Creates a new instance of
BackgroundInitializer. No externalExecutorServiceis used.
- protected BackgroundInitializer(ExecutorService exec)[source]
Creates a new instance of
BackgroundInitializerand initializes it with the givenExecutorService. If theExecutorServiceis not null, the background task for initializing this object will be scheduled at this service. Otherwise a new temporaryExecutorServiceis created.- Parameters:
exec – An external
ExecutorServiceto be used for task. execution
Methods
Creates a new builder. |
|
Gets the result of the background initialization. |
|
Gets the |
|
Gets the external |
|
Gets the |
|
Gets the number of background tasks to be created for this initializer. |
|
Gets an Exception with a type of E as defined by a concrete subclass of this class. |
|
Tests whether this instance is initialized. |
|
Tests whether this |
|
Sets an |
|
Starts the background initialization. |
- public static <T> BackgroundInitializer.Builder<BackgroundInitializer<T>, T> builder()[source]
Creates a new builder.
- Type parameters:
<T> – The type of object to build.
- Returns:
A new builder.
- Since:
3.14.0
- @Override public T get() throws ConcurrentException[source]
Gets the result of the background initialization. This method blocks until initialization is complete. If the background processing caused a runtime exception, it is directly thrown by this method. Checked exceptions, including
InterruptedExceptionare wrapped in aConcurrentException. Calling this method before#start()was called causes anIllegalStateExceptionexception to be thrown.- Returns:
The object produced by this initializer.
- Throws:
org.apache.commons.lang3.concurrent.ConcurrentException – Thrown if a checked exception occurred during background processing.
java.lang.IllegalStateException – Thrown if
#start()has not been called.
- protected final synchronized ExecutorService getActiveExecutor()[source]
Gets the
ExecutorServicethat is actually used for executing the background task. This method can be called after#start()(beforestart()it returnsnull). 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.- Returns:
The
ExecutorServicefor executing the background task.
- public final synchronized ExecutorService getExternalExecutor()[source]
Gets the external
ExecutorServiceto be used by this class.- Returns:
The
ExecutorService.
- public synchronized Future<T> getFuture()[source]
Gets the
Futureobject that was created when#start()was called. Therefore this method can only be called afterstart().
- protected int getTaskCount()[source]
Gets the number of background tasks to be created for this initializer. This information is evaluated when a temporary
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.- Returns:
The number of background tasks required by this initializer.
- @Override protected Exception getTypedException(Exception e)[source]
Gets an Exception with a type of E as defined by a concrete subclass of this class.
- Parameters:
e – The actual exception that was thrown.
- Returns:
A new exception with the actual type of E, that wraps e.
- @Override public synchronized boolean isInitialized()[source]
Tests whether this instance is initialized. Once initialized, always returns true. If initialization failed then the failure will be cached and this will never return true.
- Returns:
true if initialization completed successfully, otherwise false.
- Since:
3.14.0
- public synchronized boolean isStarted()[source]
Tests whether this
BackgroundInitializerhas already been started.- Returns:
A flag whether the
#start()method has already been called.
- public final synchronized void setExternalExecutor(ExecutorService externalExecutor)[source]
Sets an
ExecutorServiceto be used by this class. TheExecutorServicepassed to this method is used for executing the background task. Thus it is possible to re-use an already existingExecutorServiceor to use a specially configured one. If noExecutorServiceis 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.- Parameters:
externalExecutor – The
ExecutorServiceto be used.- Throws:
java.lang.IllegalStateException – Thrown if this initializer has already been started.
- public synchronized boolean start()[source]
Starts the background initialization. With this method the initializer becomes active and invokes the
#initialize()method in a background task. ABackgroundInitializercan 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.- Returns:
A flag whether the initializer could be started successfully.