[LANG-496] Added a comment why a temporary variable is used in get().
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@819146 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f4ae31ef19
commit
1e060c6e35
|
@ -42,9 +42,11 @@ package org.apache.commons.lang.concurrent;
|
|||
* to this class, a subclass of {@code LazyInitializer} has to be created:
|
||||
*
|
||||
* <pre>
|
||||
* public class ComplexObjectInitializer extends LazyInitializer<ComplexObject> {
|
||||
* public class ComplexObjectInitializer extends LazyInitializer<ComplexObject>
|
||||
* {
|
||||
* @Override
|
||||
* protected ComplexObject initialize() {
|
||||
* protected ComplexObject initialize()
|
||||
* {
|
||||
* return new ComplexObject();
|
||||
* }
|
||||
* }
|
||||
|
@ -75,7 +77,8 @@ package org.apache.commons.lang.concurrent;
|
|||
* @version $Id$
|
||||
* @param <T> the type of the object managed by this initializer class
|
||||
*/
|
||||
public abstract class LazyInitializer<T> {
|
||||
public abstract class LazyInitializer<T>
|
||||
{
|
||||
/** Stores the managed object. */
|
||||
private volatile T object;
|
||||
|
||||
|
@ -85,13 +88,19 @@ public abstract class LazyInitializer<T> {
|
|||
*
|
||||
* @return the object initialized by this {@code LazyInitializer}
|
||||
*/
|
||||
public T get() {
|
||||
public T get()
|
||||
{
|
||||
// use a temporary variable to reduce the number of reads of the
|
||||
// volatile field
|
||||
T result = object;
|
||||
|
||||
if (result == null) {
|
||||
synchronized (this) {
|
||||
if (result == null)
|
||||
{
|
||||
synchronized (this)
|
||||
{
|
||||
result = object;
|
||||
if (result == null) {
|
||||
if (result == null)
|
||||
{
|
||||
object = result = initialize();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue