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/04/27 20:57:06 UTC

svn commit: r1590457 - /logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/FileConfigurationMonitor.java

Author: mattsicker
Date: Sun Apr 27 18:57:05 2014
New Revision: 1590457

URL: http://svn.apache.org/r1590457
Log:
Improve locking usage.

  - Call System.currentTimeMillis() outside of a synchronized context.
  - Use AtomicInteger over volatile (though it would be nice if it 
  included a bit mask checking operation).
  - Use a Lock over synchronized(this).

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/FileConfigurationMonitor.java

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/FileConfigurationMonitor.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/FileConfigurationMonitor.java?rev=1590457&r1=1590456&r2=1590457&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/FileConfigurationMonitor.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/FileConfigurationMonitor.java Sun Apr 27 18:57:05 2014
@@ -18,6 +18,9 @@ package org.apache.logging.log4j.core.co
 
 import java.io.File;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 
 /**
  * Configuration monitor that periodically checks the timestamp of the configuration file and calls the
@@ -41,7 +44,9 @@ public class FileConfigurationMonitor im
 
     private long nextCheck;
 
-    private volatile int counter = 0;
+    private final AtomicInteger counter = new AtomicInteger(0);
+
+    private static final Lock LOCK = new ReentrantLock();
 
     private final Reconfigurable reconfigurable;
 
@@ -68,18 +73,19 @@ public class FileConfigurationMonitor im
      */
     @Override
     public void checkConfiguration() {
-        if ((++counter & MASK) == 0) {
-            synchronized (this) {
-                final long current = System.currentTimeMillis();
-                if (current >= nextCheck) {
-                    nextCheck = current + interval;
-                    if (file.lastModified() > lastModified) {
-                        lastModified = file.lastModified();
-                        for (final ConfigurationListener listener : listeners) {
-                            listener.onChange(reconfigurable);
-                        }
+        final long current = System.currentTimeMillis();
+        if (((counter.incrementAndGet() & MASK) == 0) && (current >= nextCheck)) {
+            LOCK.lock();
+            try {
+                nextCheck = current + interval;
+                if (file.lastModified() > lastModified) {
+                    lastModified = file.lastModified();
+                    for (final ConfigurationListener listener : listeners) {
+                        listener.onChange(reconfigurable);
                     }
                 }
+            } finally {
+                LOCK.unlock();
             }
         }
     }