// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.logserver;

import com.yahoo.logserver.handlers.LogHandler;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Bjorn Borud
 */
public class Flusher extends Thread {
    private static final Logger log = Logger.getLogger(Flusher.class.getName());
    private static final Flusher instance;
    private static final List<WeakReference<LogHandler>> logHandlers =
            new ArrayList<WeakReference<LogHandler>>();

    static {
        instance = new Flusher();
        instance.start();
    }

    Flusher() {
        super("flusher");
    }

    public static synchronized void register(LogHandler logHandler) {
        logHandlers.add(new WeakReference<>(logHandler));
    }

    @Override
    public synchronized void run() {
        try {
            while(!isInterrupted()) {
                Iterator<WeakReference<LogHandler>> it = logHandlers.iterator();
                while (it.hasNext()) {
                    WeakReference<LogHandler> r = it.next();
                    LogHandler h = r.get();
                    if (h == null) {
                        it.remove();
                    } else {
                        h.flush();
                    }
                    if (log.isLoggable(Level.FINE)) {
                        log.log(Level.FINE, "Flushing " + h);
                    }
                }
                Thread.sleep(2000);
            }
        } catch (InterruptedException e) {
            log.log(Level.WARNING, "flusher was interrupted", e);
        }
    }
}

@@ -402,23 +402,16 @@
 rud%0A */%0A
-public 
 class Fl
@@ -565,31 +565,24 @@
     private 
-static 
 final List%3CW
@@ -620,28 +620,16 @@
 ndlers =
-%0A           
  new Arr
@@ -639,33 +639,8 @@
 ist%3C
-WeakReference%3CLogHandler%3E
 %3E();
@@ -778,74 +778,196 @@
     
-public static synchronized void register(LogHandler logHandler) %7B%0A
+static void register(LogHandler logHandler) %7B%0A        instance.addHandler(logHandler);%0A    %7D%0A%0A    private void addHandler(LogHandler logHandler) %7B%0A        synchronized (logHandlers) %7B%0A    
     
@@ -1016,24 +1016,34 @@
 gHandler));%0A
+        %7D%0A
     %7D%0A%0A    @
@@ -1136,24 +1136,73 @@
 rupted()) %7B%0A
+                synchronized (logHandlers) %7B%0A    
             
@@ -1282,24 +1282,28 @@
             
+    
 while (it.ha
@@ -1333,16 +1333,20 @@
         
+ 
+   
 WeakRefe
@@ -1398,16 +1398,20 @@
         
+    
 LogHandl
@@ -1442,24 +1442,28 @@
             
+  
+  
 if (h == nul
@@ -1491,16 +1491,20 @@
         
+    
 it.remov
@@ -1528,16 +1528,20 @@
         
+   
+ 
 %7D else %7B
@@ -1541,16 +1541,20 @@
  else %7B%0A
+    
         
@@ -1592,34 +1592,42 @@
                 
+   
+ 
 %7D%0A
+    
                 
@@ -1680,32 +1680,36 @@
                 
+    
 log.log(Level.FI
@@ -1730,16 +1730,42 @@
 %22 + h);%0A
+                        %7D%0A
         

synchronize on static method and on instance method are completly disjunct. Instead you should synchonize exactly what you need.