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

public Class AtomicInitializer<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.function.FailableConsumer, .FailableSupplier

A specialized implementation of the 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:

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.

Since
3.0

Nested and Inner Type Summary

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

Builds a new instance.

Field Summary

Modifier and TypeField and Description
private static final Object
private final AtomicReference<T>
reference

Holds the reference to the managed object.

Constructor Summary

AccessConstructor and Description
public
AtomicInitializer()

Constructs a new instance.

private
AtomicInitializer(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
>
AtomicInitializer.Builder<AtomicInitializer<T>, T>

Returns:

a new builder.
builder
()

Creates a new builder.

public T

Returns:

the object created by this AtomicInitializer.
get
()

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

Gets the object managed by this initializer.

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

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

AtomicInitializerback to summary
public AtomicInitializer()

Constructs a new instance.

AtomicInitializerback to summary
private AtomicInitializer(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> AtomicInitializer.Builder<AtomicInitializer<T>, T> builder()

Creates a new builder.

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

a new builder.

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

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

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.

Returns:T

the object created by this AtomicInitializer.

Annotations
@Override
Exceptions
ConcurrentException:
if an error occurred during initialization of the object.
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 AtomicInitializer.Builder<I extends AtomicInitializer<T>, T>

extends AbstractConcurrentInitializer.AbstractBuilder<I extends AtomicInitializer<T>, T, AtomicInitializer.Builder<I extends AtomicInitializer<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