ConcurrentInitializer implementation which is similar
to AtomicInitializer, but ensures that the initialize()
method is called only once.
As AtomicInitializer this class is based on atomic variables, so it
can create an object under concurrent access without synchronization.
However, it implements an additional check to guarantee that the
initialize() method which actually creates the object cannot be
called multiple times.
Because of this additional check this implementation is slightly less
efficient than AtomicInitializer, but if the object creation in the
initialize() method is expensive or if multiple invocations of
initialize() are problematic, it is the better alternative.
From its semantics this class has the same properties as
LazyInitializer. It is a "save" implementation of the lazy
initializer pattern. Comparing both classes in terms of efficiency is
difficult because which one is faster depends on multiple factors. Because
AtomicSafeInitializer does not use synchronization at all it probably
outruns LazyInitializer, at least under low or moderate concurrent
access. Developers should run their own benchmarks on the expected target
platform to decide which implementation is suitable for their specific use
case.
| Modifier and Type | Class and Description |
|---|---|
| public static class | AtomicSafeInitializer.
The type of the initializer managed by this builder. I extends AtomicSafeInitializerThe type of results supplied by this builder. T>Builds a new instance. |
| Modifier and Type | Field and Description |
|---|---|
| private final AtomicReference | factory
A guard which ensures that initialize() is called only once. |
| private static final Object | |
| private final AtomicReference | reference
Holds the reference to the managed object. |
| Access | Constructor and Description |
|---|---|
| public | |
| private | AtomicSafeInitializer(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> AtomicSafeInitializer. | |
| public final T | Returns: lazily initialized object.Implements org. Gets (and initialize, if not initialized yet) the required object. |
| 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. |
| factory | back to summary |
|---|---|
| private final AtomicReference<AtomicSafeInitializer<T>> factory A guard which ensures that initialize() is called only once. | |
| 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. | |
| AtomicSafeInitializer | back to summary |
|---|---|
| public AtomicSafeInitializer() Constructs a new instance. | |
| AtomicSafeInitializer | back to summary |
|---|---|
| private AtomicSafeInitializer(final FailableSupplier<T, ConcurrentException> initializer, final FailableConsumer<T, ConcurrentException> closer) Constructs a new instance.
| |
| builder | back to summary |
|---|---|
| public static <T> AtomicSafeInitializer. Creates a new builder.
| |
| get | back to summary |
|---|---|
| public final T get() throws ConcurrentException Implements org. Gets (and initialize, if not initialized yet) the required 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
| |