ConcurrentInitializer interface
based on an AtomicReference variable.
This class maintains a member field of type AtomicReference. It
implements the following algorithm to create and initialize an object in its
get() method:
AtomicReference variable contains
already a value. If this is the case, the value is directly returned.initialize() method is called. This method must be
defined in concrete subclasses to actually create the managed object.initialize() it is checked
whether the AtomicReference variable is still undefined. This has to
be done because in the meantime another thread may have initialized the
object. If the reference is still empty, the newly created object is stored
in it and returned by this method.AtomicReference is returned.
Because atomic variables are used this class does not need any
synchronization. So there is no danger of deadlock, and access to the managed
object is efficient. However, if multiple threads access the AtomicInitializer object before it has been initialized almost at the same
time, it can happen that initialize() is called multiple times. The
algorithm outlined above guarantees that get() always returns the
same object though.
Compared with the LazyInitializer class, this class can be more
efficient because it does not need synchronization. The drawback is that the
initialize() method can be called multiple times which may be
problematic if the creation of the managed object is expensive. As a rule of
thumb this initializer implementation is preferable if there are not too many
threads involved and the probability that multiple threads access an
uninitialized object is small. If there is high parallelism,
LazyInitializer is more appropriate.
| Modifier and Type | Class and Description |
|---|---|
| public static class | AtomicInitializer.
The type of the initializer managed by this builder. I extends AtomicInitializerThe type of results supplied by this builder. T>Builds a new instance. |
| Modifier and Type | Field and Description |
|---|---|
| private static final Object | |
| private final AtomicReference | reference
Holds the reference to the managed object. |
| Access | Constructor and Description |
|---|---|
| public | |
| private | AtomicInitializer(final FailableSupplier<T, ConcurrentException>
the initializer supplier called by initializer, final FailableConsumer<T, ConcurrentException> initialize().the closer consumer called by closer)close().Constructs a new instance. |
| Modifier and Type | Method and Description |
|---|---|
| public static < the type of object to build. T> AtomicInitializer. | |
| public T | Returns: the object created by thisAtomicInitializer.Implements org. Gets the object managed by this initializer. |
| private T | |
| protected ConcurrentException | getTypedException(final Exception e)
Implements abstract org. Gets an Exception with a type of E as defined by a concrete subclass of this class. |
| public boolean | Returns: whether this instance is initialized. Once initialized, always returns true.Implements abstract org. Tests whether this instance is initialized. |
| NO_INIT | back to summary |
|---|---|
| private static final Object NO_INIT | |
| reference | back to summary |
|---|---|
| private final AtomicReference<T> reference Holds the reference to the managed object. | |
| AtomicInitializer | back to summary |
|---|---|
| public AtomicInitializer() Constructs a new instance. | |
| AtomicInitializer | back to summary |
|---|---|
| private AtomicInitializer(final FailableSupplier<T, ConcurrentException> initializer, final FailableConsumer<T, ConcurrentException> closer) Constructs a new instance.
| |
| builder | back to summary |
|---|---|
| public static <T> AtomicInitializer. Creates a new builder.
| |
| get | back to summary |
|---|---|
| public T get() throws ConcurrentException Implements org. Gets the object managed by this initializer. The object is created if it is not available yet and stored internally. This method always returns the same object.
| |
| getNoInit | back to summary |
|---|---|
| private T getNoInit() Gets the internal no-init object cast for this instance.
| |
| getTypedException | back to summary |
|---|---|
| protected ConcurrentException getTypedException(final Exception e) Implements abstract org. Doc from org. Gets an Exception with a type of E as defined by a concrete subclass of this class.
| |
| isInitialized | back to summary |
|---|---|
| public boolean isInitialized() Implements abstract org. Tests whether this instance is initialized. Once initialized, always returns true.
| |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public I |
| Builder | back to summary |
|---|---|
| public Builder() Constructs a new instance. | |
| get | back to summary |
|---|---|
| public I get() Implements org. Doc from org. Supplies an object
| |