felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1370708 - /felix/trunk/log/src/main/java/org/apache/felix/log/LogListenerThread.java
Date Wed, 08 Aug 2012 08:50:34 GMT
Author: jawi
Date: Wed Aug  8 08:50:34 2012
New Revision: 1370708

URL: http://svn.apache.org/viewvc?rev=1370708&view=rev
Log:
FELIX-3623: made the locks used to notify listeners of new log entries as small as possible
by breaking them in separate locks. Also notify all listeners of all current log entries,
instead of the 'single delivery' policy used before.

Modified:
    felix/trunk/log/src/main/java/org/apache/felix/log/LogListenerThread.java

Modified: felix/trunk/log/src/main/java/org/apache/felix/log/LogListenerThread.java
URL: http://svn.apache.org/viewvc/felix/trunk/log/src/main/java/org/apache/felix/log/LogListenerThread.java?rev=1370708&r1=1370707&r2=1370708&view=diff
==============================================================================
--- felix/trunk/log/src/main/java/org/apache/felix/log/LogListenerThread.java (original)
+++ felix/trunk/log/src/main/java/org/apache/felix/log/LogListenerThread.java Wed Aug  8 08:50:34
2012
@@ -33,8 +33,6 @@ import org.osgi.service.log.LogListener;
  */
 final class LogListenerThread extends Thread
 {
-    // Whether the thread is stopping or not.
-    private boolean m_stopping = false;
     // The list of entries waiting to be delivered to the log listeners.
     private final List m_entriesToDeliver = new ArrayList();
     // The list of listeners.
@@ -87,7 +85,10 @@ final class LogListenerThread extends Th
      */
     int getListenerCount()
     {
-        return m_listeners.size();
+        synchronized (m_listeners)
+        {
+            return m_listeners.size();
+        }
     }
 
     /**
@@ -97,8 +98,7 @@ final class LogListenerThread extends Th
     {
         synchronized (m_entriesToDeliver)
         {
-            m_stopping = true;
-            m_entriesToDeliver.notifyAll();
+            interrupt();
         }
     }
 
@@ -108,34 +108,11 @@ final class LogListenerThread extends Th
      */
     public void run()
     {
-        boolean stop = false;
-
-        for (; !stop;)
+        while (!isInterrupted())
         {
+            List entriesToDeliver = new ArrayList();
             synchronized (m_entriesToDeliver)
             {
-                if (!m_entriesToDeliver.isEmpty())
-                {
-                    LogEntry entry = (LogEntry) m_entriesToDeliver.remove(0);
-
-                    synchronized (m_listeners)
-                    {
-                        Iterator listenerIt = m_listeners.iterator();
-                        while (listenerIt.hasNext())
-                        {
-                            try
-                            {
-                                LogListener listener = (LogListener) listenerIt.next();
-                                listener.logged(entry);
-                            }
-                            catch (Throwable t)
-                            {
-                                // catch and discard any exceptions thrown by the listener
-                            }
-                        }
-                    }
-                }
-
                 if (m_entriesToDeliver.isEmpty())
                 {
                     try
@@ -144,14 +121,49 @@ final class LogListenerThread extends Th
                     }
                     catch (InterruptedException e)
                     {
-                        // do nothing
+                        // the interrupt-flag is cleared; so, let's play nice and 
+                        // interrupt this thread again to stop it...
+                        interrupt();
                     }
                 }
+                else 
+                {
+                    // Copy all current entries and deliver them in a single go...
+                    entriesToDeliver.addAll(m_entriesToDeliver);
+                    m_entriesToDeliver.clear();
+                }
             }
-
-            if (m_stopping)
+            
+            if (!entriesToDeliver.isEmpty())
             {
-                stop = true;
+                // Take a snapshot of all current listeners and deliver all
+                // pending messages to them...
+                List listeners = new ArrayList();
+                synchronized (m_listeners) 
+                {
+                    listeners.addAll(m_listeners);
+                }
+
+                Iterator entriesIt = entriesToDeliver.iterator();
+                while (entriesIt.hasNext()) 
+                {
+                    LogEntry entry = (LogEntry) entriesIt.next();
+                    
+                    Iterator listenerIt = listeners.iterator();
+                    while (listenerIt.hasNext())
+                    {
+                        LogListener listener = (LogListener) listenerIt.next();
+                        
+                        try
+                        {
+                            listener.logged(entry);
+                        }
+                        catch (Throwable t)
+                        {
+                            // catch and discard any exceptions thrown by the listener
+                        }
+                    }
+                }
             }
         }
     }



Mime
View raw message