Author: tilman
Date: Mon Dec 28 12:49:54 2020
New Revision: 1884866

URL: http://svn.apache.org/viewvc?rev=1884866&view=rev
Log:
PDFBOX-5056: fix double-checked locking, as suggested by Mike Kaplinskiy

Modified:
    
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/encoding/Encoding.java

Modified: 
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/encoding/Encoding.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/encoding/Encoding.java?rev=1884866&r1=1884865&r2=1884866&view=diff
==============================================================================
--- 
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/encoding/Encoding.java
 (original)
+++ 
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/encoding/Encoding.java
 Mon Dec 28 12:49:54 2020
@@ -63,7 +63,7 @@ public abstract class Encoding implement
 
     protected final Map<Integer, String> codeToName = new HashMap<Integer, 
String>(250);
     protected final Map<String, Integer> inverted = new HashMap<String, 
Integer>(250);
-    private Set<String> names;
+    private volatile Set<String> names;
 
     /**
      * Returns an unmodifiable view of the code -&gt; name mapping.
@@ -140,11 +140,13 @@ public abstract class Encoding implement
         {
             synchronized(this)
             {
-                // PDFBOX-3404: avoid possibility that one thread ends up with 
newly created empty map from other thread
-                Set<String> tmpSet = new HashSet<String>(codeToName.values());
-                // make sure that assignment is done after initialisation is 
complete
-                names = tmpSet;
-                // note that it might still happen that 'names' is initialized 
twice, but this is harmless
+                if (names == null)
+                {
+                    // PDFBOX-3404: avoid possibility that one thread ends up 
with newly created empty map from other thread
+                    Set<String> tmpSet = new 
HashSet<String>(codeToName.values());
+                    // make sure that assignment is done after initialisation 
is complete
+                    names = tmpSet;
+                }
             }
             // at this point, names will never be null.
         }


Reply via email to