You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/09/15 01:26:52 UTC

[03/21] git commit: Use double checked locking.

Use double checked locking.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/1d0e634a
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/1d0e634a
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/1d0e634a

Branch: refs/heads/master
Commit: 1d0e634a8ef6a8285dec9496cbe26b5209f334aa
Parents: 30dec3d
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 14 12:34:33 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 14 12:34:33 2014 -0500

----------------------------------------------------------------------
 .../config/plugins/util/PluginRegistry.java     | 42 +++++++++++---------
 1 file changed, 23 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1d0e634a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginRegistry.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginRegistry.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginRegistry.java
index e7cd19c..9b1060b 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginRegistry.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginRegistry.java
@@ -17,18 +17,6 @@
 
 package org.apache.logging.log4j.core.config.plugins.util;
 
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.config.plugins.Plugin;
-import org.apache.logging.log4j.core.config.plugins.PluginAliases;
-import org.apache.logging.log4j.core.config.plugins.processor.PluginCache;
-import org.apache.logging.log4j.core.config.plugins.processor.PluginEntry;
-import org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor;
-import org.apache.logging.log4j.core.util.ClassLoaderResourceLoader;
-import org.apache.logging.log4j.core.util.Loader;
-import org.apache.logging.log4j.core.util.ResourceLoader;
-import org.apache.logging.log4j.status.StatusLogger;
-import org.apache.logging.log4j.util.Strings;
-
 import java.io.IOException;
 import java.net.URI;
 import java.net.URL;
@@ -43,6 +31,18 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.atomic.AtomicReference;
 
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAliases;
+import org.apache.logging.log4j.core.config.plugins.processor.PluginCache;
+import org.apache.logging.log4j.core.config.plugins.processor.PluginEntry;
+import org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor;
+import org.apache.logging.log4j.core.util.ClassLoaderResourceLoader;
+import org.apache.logging.log4j.core.util.Loader;
+import org.apache.logging.log4j.core.util.ResourceLoader;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.Strings;
+
 /**
  * Registry singleton for PluginType maps partitioned by source type and then by category names.
  */
@@ -50,6 +50,9 @@ public class PluginRegistry {
 
     private static final Logger LOGGER = StatusLogger.getLogger();
 
+    private static volatile PluginRegistry INSTANCE;
+    private static final Object INSTANCE_LOCK = new Object();
+
     /**
      * Contains plugins found in Log4j2Plugins.dat cache files in the main CLASSPATH.
      */
@@ -71,12 +74,6 @@ public class PluginRegistry {
     private PluginRegistry() {
     }
 
-    private static class Holder {
-        // the usual initialization-on-demand holder idiom
-        // https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
-        private static final PluginRegistry INSTANCE = new PluginRegistry();
-    }
-
     /**
      * Returns the global PluginRegistry instance.
      *
@@ -84,7 +81,14 @@ public class PluginRegistry {
      * @since 2.1
      */
     public static PluginRegistry getInstance() {
-        return Holder.INSTANCE;
+        if (INSTANCE == null) {
+            synchronized (INSTANCE_LOCK) {
+                if (INSTANCE == null) {
+                    INSTANCE = new PluginRegistry();
+                }
+            }
+        }
+        return INSTANCE;
     }
 
     /**