Top Description Inners Fields Constructors Methods
org.apache.commons.lang3.concurrent

public Class AtomicSafeInitializer<T>

extends AbstractConcurrentInitializer<T, ConcurrentException>
Class Inheritance
Type Parameters
<T>
the type of the object managed by this initializer class
Imports
java.util.concurrent.atomic.AtomicReference, org.apache.commons.lang3.exception.ExceptionUtils, org.apache.commons.lang3.function.FailableConsumer, .FailableSupplier

A specialized 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.

Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
AtomicSafeInitializer.Builder<
The type of the initializer managed by this builder.
I extends AtomicSafeInitializer<T>
,
The type of results supplied by this builder.
T
>

Builds a new instance.

Field Summary

Modifier and TypeField and Description
private final AtomicReference<AtomicSafeInitializer<T>>
factory

A guard which ensures that initialize() is called only once.

private static final Object
private final AtomicReference<T>
reference

Holds the reference to the managed object.

Constructor Summary

AccessConstructor and Description
public
AtomicSafeInitializer()

Constructs a new instance.

private
AtomicSafeInitializer(final FailableSupplier<T, ConcurrentException>
the initializer supplier called by initialize().
initializer
,
final FailableConsumer<T, ConcurrentException>
the closer consumer called by close().
closer
)

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
public static <
the type of object to build.
T
>
AtomicSafeInitializer.Builder<AtomicSafeInitializer<T>, T>

Returns:

a new builder.
builder
()

Creates a new builder.

public final T

Returns:

lazily initialized object.
get
()

Implements org.apache.commons.lang3.function.FailableSupplier.get.

Gets (and initialize, if not initialized yet) the required object.

private T
getNoInit()

Gets the internal no-init object cast for this instance.

protected ConcurrentException
getTypedException(final Exception e)

Implements abstract org.apache.commons.lang3.concurrent.AbstractConcurrentInitializer.getTypedException.

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.
isInitialized
()

Implements abstract org.apache.commons.lang3.concurrent.AbstractConcurrentInitializer.isInitialized.

Tests whether this instance is initialized.

Inherited from org.apache.commons.lang3.concurrent.AbstractConcurrentInitializer:
closeinitialize

Field Detail

factoryback to summary
private final AtomicReference<AtomicSafeInitializer<T>> factory

A guard which ensures that initialize() is called only once.

NO_INITback to summary
private static final Object NO_INIT
referenceback to summary
private final AtomicReference<T> reference

Holds the reference to the managed object.

Constructor Detail

AtomicSafeInitializerback to summary
public AtomicSafeInitializer()

Constructs a new instance.

AtomicSafeInitializerback to summary
private AtomicSafeInitializer(final FailableSupplier<T, ConcurrentException> initializer, final FailableConsumer<T, ConcurrentException> closer)

Constructs a new instance.

Parameters
initializer:FailableSupplier<T, ConcurrentException>

the initializer supplier called by initialize().

closer:FailableConsumer<T, ConcurrentException>

the closer consumer called by close().

Method Detail

builderback to summary
public static <T> AtomicSafeInitializer.Builder<AtomicSafeInitializer<T>, T> builder()

Creates a new builder.

Parameters
<T>
the type of object to build.
Returns:AtomicSafeInitializer.Builder<AtomicSafeInitializer<T>, T>

a new builder.

Since
3.14.0
getback to summary
public final T get() throws ConcurrentException

Implements org.apache.commons.lang3.function.FailableSupplier.get.

Gets (and initialize, if not initialized yet) the required object.

Returns:T

lazily initialized object.

Annotations
@Override
Exceptions
ConcurrentException:
if the initialization of the object causes an exception.
getNoInitback to summary
private T getNoInit()

Gets the internal no-init object cast for this instance.

Annotations
@SuppressWarnings:unchecked
getTypedExceptionback to summary
protected ConcurrentException getTypedException(final Exception e)

Implements abstract org.apache.commons.lang3.concurrent.AbstractConcurrentInitializer.getTypedException.

Doc from org.apache.commons.lang3.concurrent.AbstractConcurrentInitializer.getTypedException.

Gets an Exception with a type of E as defined by a concrete subclass of this class.

Annotations
@Override
isInitializedback to summary
public boolean isInitialized()

Implements abstract org.apache.commons.lang3.concurrent.AbstractConcurrentInitializer.isInitialized.

Tests whether this instance is initialized. Once initialized, always returns true.

Returns:boolean

whether this instance is initialized. Once initialized, always returns true.

Annotations
@Override
Since
3.14.0
org.apache.commons.lang3.concurrent back to summary

public Class AtomicSafeInitializer.Builder<I extends AtomicSafeInitializer<T>, T>

extends AbstractConcurrentInitializer.AbstractBuilder<I extends AtomicSafeInitializer<T>, T, AtomicSafeInitializer.Builder<I extends AtomicSafeInitializer<T>, T>, ConcurrentException>
Class Inheritance
Type Parameters
<I>
The type of the initializer managed by this builder.
<T>
The type of results supplied by this builder.

Builds a new instance.
Since
3.14.0

Constructor Summary

AccessConstructor and Description
public
Builder()

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
public I
Inherited from org.apache.commons.lang3.concurrent.AbstractConcurrentInitializer.AbstractBuilder:
getClosergetInitializersetClosersetInitializer

Constructor Detail

Builderback to summary
public Builder()

Constructs a new instance.

Method Detail

getback to summary
public I get()

Implements org.apache.commons.lang3.function.FailableSupplier.get.

Doc from org.apache.commons.lang3.function.FailableSupplier.get.

Supplies an object

Annotations
@SuppressWarnings:unchecked
@Override