org.apache.commons.lang3.concurrent
Class AtomicInitializer<T>

java.lang.Object
  extended by org.apache.commons.lang3.concurrent.AtomicInitializer<T>
Type Parameters:
T - the type of the object managed by this initializer class
All Implemented Interfaces:
ConcurrentInitializer<T>

public abstract class AtomicInitializer<T>
extends Object
implements ConcurrentInitializer<T>

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
Version:
$Id: AtomicInitializer.java 1088899 2011-04-05 05:31:27Z bayard $

Constructor Summary
AtomicInitializer()
           
 
Method Summary
 T get()
          Returns the object managed by this initializer.
protected abstract  T initialize()
          Creates and initializes the object managed by this AtomicInitializer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AtomicInitializer

public AtomicInitializer()
Method Detail

get

public T get()
      throws ConcurrentException
Returns 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.

Specified by:
get in interface ConcurrentInitializer<T>
Returns:
the object created by this AtomicInitializer
Throws:
ConcurrentException - if an error occurred during initialization of the object

initialize

protected abstract T initialize()
                         throws ConcurrentException
Creates and initializes the object managed by this AtomicInitializer. This method is called by get() when the managed object is not available yet. An implementation can focus on the creation of the object. No synchronization is needed, as this is already handled by get(). As stated by the class comment, it is possible that this method is called multiple times.

Returns:
the managed data object
Throws:
ConcurrentException - if an error occurs during object creation


Copyright © 2001-2011 The Apache Software Foundation. All Rights Reserved.