T- the type of the object managed by this initializer class
public abstract class AtomicInitializer<T> extends java.lang.Object implements ConcurrentInitializer<T>
A specialized implementation of the
based on an
This class maintains a member field of type
implements the following algorithm to create and initialize an object in its
AtomicReferencevariable 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
AtomicReferencevariable 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.
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.
|Constructor and Description|
public T get() throws ConcurrentException
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"