You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2009/09/26 17:03:06 UTC
svn commit: r819146 -
/commons/proper/lang/trunk/src/java/org/apache/commons/lang/concurrent/LazyInitializer.java
Author: oheger
Date: Sat Sep 26 15:03:06 2009
New Revision: 819146
URL: http://svn.apache.org/viewvc?rev=819146&view=rev
Log:
[LANG-496] Added a comment why a temporary variable is used in get().
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/concurrent/LazyInitializer.java
Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/concurrent/LazyInitializer.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/concurrent/LazyInitializer.java?rev=819146&r1=819145&r2=819146&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/concurrent/LazyInitializer.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/concurrent/LazyInitializer.java Sat Sep 26 15:03:06 2009
@@ -42,9 +42,11 @@
* 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 @@
* @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 @@
*
* @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();
}
}