/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */
package org.apache.logging.log4j.core.config;

import java.io.File;
import java.util.List;

/**
 * Configuration monitor that periodically checks the timestamp of the configuration file and calls the
 * ConfigurationListeners when an update occurs.
 */
public class FileConfigurationMonitor implements ConfigurationMonitor {

    private static final int MASK = 0x0f;

    private static final int MIN_INTERVAL = 5;

    private static final int MILLIS_PER_SECOND = 1000;

    private final File file;

    private long lastModified;

    private final List<ConfigurationListener> listeners;

    private final int interval;

    private long nextCheck;

    private volatile int counter = 0;

    private final Reconfigurable reconfigurable;

    /**
     * Constructor.
     * @param reconfigurable The Configuration that can be reconfigured.
     * @param file The File to monitor.
     * @param listeners The List of ConfigurationListeners to notify upon a change.
     * @param interval The monitor interval in seconds. The minimum interval is 5 seconds.
     */
    public FileConfigurationMonitor(final Reconfigurable reconfigurable, final File file,
                                    final List<ConfigurationListener> listeners,
                                    final int interval) {
        this.reconfigurable = reconfigurable;
        this.file = file;
        this.lastModified = file.lastModified();
        this.listeners = listeners;
        this.interval = (interval < MIN_INTERVAL ? MIN_INTERVAL : interval) * MILLIS_PER_SECOND;
        this.nextCheck = System.currentTimeMillis() + interval;
    }

    /**
     * Called to determine if the configuration has changed.
     */
    @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);
                        }
                    }
                }
            }
        }
    }
}

@@ -884,16 +884,155 @@
 il.List;
+%0Aimport java.util.concurrent.atomic.AtomicInteger;%0Aimport java.util.concurrent.locks.Lock;%0Aimport java.util.concurrent.locks.ReentrantLock;
 %0A%0A/**%0A *
@@ -1604,32 +1604,117 @@
 ate 
-volatile int counter = 0
+final AtomicInteger counter = new AtomicInteger(0);%0A%0A    private static final Lock LOCK = new ReentrantLock()
 ;%0A%0A 
@@ -2774,89 +2774,8 @@
 ) %7B%0A
-        if ((++counter & MASK) == 0) %7B%0A            synchronized (this) %7B%0A        
     
@@ -2835,26 +2835,64 @@
         
-        if
+if (((counter.incrementAndGet() & MASK) == 0) &&
  (curren
@@ -2906,16 +2906,17 @@
 xtCheck)
+)
  %7B%0A     
@@ -2909,32 +2909,68 @@
 heck)) %7B%0A       
+     LOCK.lock();%0A            try %7B%0A
              nex
@@ -2966,16 +2966,19 @@
         
+   
 nextChec
@@ -3001,20 +3001,16 @@
 terval;%0A
-    
         
@@ -3079,20 +3079,16 @@
         
-    
 lastModi
@@ -3135,20 +3135,16 @@
         
-   
- 
 for (fin
@@ -3216,20 +3216,16 @@
         
-    
 listener
@@ -3263,36 +3263,32 @@
                 
-    
 %7D%0A              
@@ -3275,32 +3275,40 @@
     %7D%0A          
+      %7D%0A
           %7D%0A    
@@ -3293,33 +3293,45 @@
     %7D%0A          
-%7D
+  %7D finally %7B
 %0A               
@@ -3323,33 +3323,46 @@
                 
-%7D
+LOCK.unlock();
 %0A            %7D%0A 

Improve locking usage.