diff --git a/src/java/org/apache/commons/lang/concurrent/LazyInitializer.java b/src/java/org/apache/commons/lang/concurrent/LazyInitializer.java index 211afc702..efcba9bb0 100644 --- a/src/java/org/apache/commons/lang/concurrent/LazyInitializer.java +++ b/src/java/org/apache/commons/lang/concurrent/LazyInitializer.java @@ -42,9 +42,11 @@ package org.apache.commons.lang.concurrent; * to this class, a subclass of {@code LazyInitializer} has to be created: * *
- * 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$ * @paramthe type of the object managed by this initializer class */ -public abstract class LazyInitializer { +public abstract class LazyInitializer +{ /** Stores the managed object. */ private volatile T object; @@ -85,13 +88,19 @@ public abstract class LazyInitializer { * * @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(); } }