internal refactor: use ConcurrentHashMap instead of HashMap + synchronized blocks
@@ -20,11 +20,11 @@ package org.apache.wiki;
import java.lang.ref.WeakReference;
import java.util.EmptyStackException;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
+import java.util.concurrent.ConcurrentHashMap;
import org.apache.log4j.Logger;
@@ -58,7 +58,7 @@ public final class WatchDog {
private static final Logger log = Logger.getLogger( WatchDog.class );
- private static HashMap< Integer, WeakReference< WatchDog > > c_kennel = new HashMap< Integer, WeakReference< WatchDog > >();
+ private static Map< Integer, WeakReference< WatchDog > > c_kennel = new ConcurrentHashMap< Integer, WeakReference< WatchDog > >();
private static WikiBackgroundThread c_watcherThread;
@@ -86,10 +86,8 @@ public final class WatchDog {
wd = new WatchDog( engine, t );
w = new WeakReference< WatchDog >( wd );
- synchronized( c_kennel ) {
c_kennel.put( t.hashCode(), w );
}
- }
return wd;
}
@@ -136,10 +134,7 @@ public final class WatchDog {
return;
}
- synchronized( c_kennel ) {
- for( Iterator< Map.Entry< Integer, WeakReference< WatchDog > > > i = c_kennel.entrySet().iterator(); i.hasNext(); ) {
- Map.Entry< Integer, WeakReference< WatchDog > > e = i.next();
-
+ for( Map.Entry< Integer, WeakReference< WatchDog > > e : c_kennel.entrySet() ) {
WeakReference< WatchDog > w = e.getValue();
//
@@ -152,7 +147,6 @@ public final class WatchDog {
}
}
}
- }
/**
* Can be used to enable the WatchDog. Will cause a new Thread to be created, if none was existing previously.
@@ -373,10 +367,7 @@ public final class WatchDog {
return;
}
- synchronized( c_kennel ) {
- for( Iterator< Map.Entry< Integer, WeakReference< WatchDog > > > i = c_kennel.entrySet().iterator(); i.hasNext(); ) {
- Map.Entry< Integer, WeakReference< WatchDog > > entry = i.next();
-
+ for( Map.Entry< Integer, WeakReference< WatchDog > > entry : c_kennel.entrySet() ) {
WeakReference< WatchDog > wr = entry.getValue();
WatchDog w = wr.get();
if( w != null ) {
@@ -387,8 +378,7 @@ public final class WatchDog {
break;
}
}
- } // for
- } // synchronized
+ }
WatchDog.scrub();
}
