BUG-1851: fix potential visibility problem 25/11125/1
authorRobert Varga <rovarga@cisco.com>
Fri, 12 Sep 2014 22:17:13 +0000 (00:17 +0200)
committerRobert Varga <rovarga@cisco.com>
Fri, 12 Sep 2014 22:19:29 +0000 (00:19 +0200)
formattedRevision is a normal field, which may be reordered. Make it
volatile and initialize it as needed. Format operation is simple enough
so we do not require a full synchronization, worst that can happen is
that two racing threads will format it twice.

Change-Id: I52bf2f22cccd9dbb115858dd98f08bc63c3001b6
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java

index bec56bf76fe77f54467ab5fc00e46d9f03eb1a59..452b2fa99e78965f5949a83801f738db137be34b 100644 (file)
@@ -11,7 +11,6 @@ import java.io.Serializable;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Date;
-
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.objcache.ObjectCache;
 import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
@@ -31,7 +30,7 @@ public final class QNameModule implements Immutable, Serializable {
     private final Date revision;
 
     //Nullable
-    private String formattedRevision;
+    private volatile String formattedRevision;
 
     private QNameModule(final URI namespace, final Date revision) {
         this.namespace = namespace;
@@ -68,15 +67,13 @@ public final class QNameModule implements Immutable, Serializable {
             return null;
         }
 
-        if (formattedRevision == null) {
-            synchronized (this) {
-                if (formattedRevision == null) {
-                    formattedRevision = SimpleDateFormatUtil.getRevisionFormat().format(revision);
-                }
-            }
+        String ret = formattedRevision;
+        if (ret == null) {
+            ret = SimpleDateFormatUtil.getRevisionFormat().format(revision);
+            formattedRevision = ret;
         }
 
-        return formattedRevision;
+        return ret;
     }
 
     /**