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

public Class LazyInitializer<T>

extends AbstractConcurrentInitializer<T, ConcurrentException>
Class Inheritance
Type Parameters
<T>
the type of the object managed by the initializer.
Imports
org.apache.commons.lang3.function.FailableConsumer, .FailableSupplier

This class provides a generic implementation of the lazy initialization pattern.

Sometimes an application has to deal with an object only under certain circumstances, e.g. when the user selects a specific menu item or if a special event is received. If the creation of the object is costly or the consumption of memory or other system resources is significant, it may make sense to defer the creation of this object until it is really needed. This is a use case for the lazy initialization pattern.

This abstract base class provides an implementation of the double-check idiom for an instance field as discussed in Joshua Bloch's "Effective Java", 2nd edition, item 71. The class already implements all necessary synchronization. A concrete subclass has to implement the initialize() method, which actually creates the wrapped data object.

As an usage example consider that we have a class ComplexObject whose instantiation is a complex operation. In order to apply lazy initialization to this class, a subclass of LazyInitializer has to be created:

public class ComplexObjectInitializer extends LazyInitializer<ComplexObject> {
    @Override
    protected ComplexObject initialize() {
        return new ComplexObject();
    }
}

Access to the data object is provided through the get() method. So, code that wants to obtain the ComplexObject instance would simply look like this:

// Create an instance of the lazy initializer
ComplexObjectInitializer initializer = new ComplexObjectInitializer();
...
// When the object is actually needed:
ComplexObject cobj = initializer.get();

If multiple threads call the get() method when the object has not yet been created, they are blocked until initialization completes. The algorithm guarantees that only a single instance of the wrapped object class is created, which is passed to all callers. Once initialized, calls to the get() method are pretty fast because no synchronization is needed (only an access to a volatile member field).

Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
LazyInitializer.Builder<
The type of the initializer managed by this builder.
I extends LazyInitializer<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
NO_INIT

A unique value indicating an un-initialized instance.

private volatile T
object

Stores the managed object.

Constructor Summary

AccessConstructor and Description
public
LazyInitializer()

Constructs a new instance.

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

Returns:

a new builder.
builder
()

Creates a new builder.

public T

Returns:

the object initialized by this LazyInitializer.
get
()

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

Gets the object wrapped by 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

A unique value indicating an un-initialized instance.

objectback to summary
private volatile T object

Stores the managed object.

Annotations
@SuppressWarnings:unchecked

Constructor Detail

LazyInitializerback to summary
public LazyInitializer()

Constructs a new instance.

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

Creates a new builder.

Parameters
<T>
the type of object to build.
Returns:LazyInitializer.Builder<LazyInitializer<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 wrapped by this instance. On first access the object is created. After that it is cached and can be accessed pretty fast.

Returns:T

the object initialized by this LazyInitializer.

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

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