[logback-dev] svn commit: r1602 - in logback/trunk/logback-core/src/main/java/ch/qos/logback/core: . util
noreply.ceki at qos.ch
noreply.ceki at qos.ch
Tue Nov 13 11:41:22 CET 2007
Author: ceki
Date: Tue Nov 13 11:41:22 2007
New Revision: 1602
Modified:
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/BasicStatusManager.java
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/StatusPrinter.java
Log:
Synchronizing on ConcurrentModificationException in order to avoid ConcurrentModificationException
exceptions on the list maintained by ConcurrentModificationException, as shown below
java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
at java.util.AbstractList$Itr.next(AbstractList.java:420)
at ch.qos.logback.core.util.StatusPrinter.print(StatusPrinter.java:47)
at ch.qos.logback.core.util.StatusPrinter.print(StatusPrinter.java:38)
at ch.qos.logback.audit.server.AuditServerTest.testUnreachable(AuditServerTest.java:137)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
This can happen if multiple thread access BasicStatusManager simultaneously.
Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/BasicStatusManager.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/BasicStatusManager.java (original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/BasicStatusManager.java Tue Nov 13 11:41:22 2007
@@ -9,7 +9,6 @@
*/
package ch.qos.logback.core;
-
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -20,19 +19,22 @@
public class BasicStatusManager implements StatusManager {
public static final int MAX_COUNT = 200;
-
+
int count = 0;
List<Status> statusList = new ArrayList<Status>();
int level = Status.INFO;
-
- public void add(Status newStatus) {
- //System.out.println(newStatus);
- if(count > MAX_COUNT) {
+
+ // This method is synchronized on the instance.
+ // Code iterating on this.iterator is expected to
+ // also synchronize on this (the BasicStatusManager instance)
+ public synchronized void add(Status newStatus) {
+ // System.out.println(newStatus);
+ if (count > MAX_COUNT) {
return;
}
count++;
-
- if(newStatus.getLevel() > level) {
+
+ if (newStatus.getLevel() > level) {
level = newStatus.getLevel();
}
statusList.add(newStatus);
@@ -47,8 +49,7 @@
}
public int getCount() {
- return count;
+ return count;
}
-
}
Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/StatusPrinter.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/StatusPrinter.java (original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/StatusPrinter.java Tue Nov 13 11:41:22 2007
@@ -41,12 +41,12 @@
}
public static void print(StatusManager sm) {
-
- Iterator it = sm.iterator();
- while (it.hasNext()) {
- Status s = (Status) it.next();
- print("", s);
-
+ synchronized (sm) {
+ Iterator it = sm.iterator();
+ while (it.hasNext()) {
+ Status s = (Status) it.next();
+ print("", s);
+ }
}
}
More information about the logback-dev
mailing list