From db3ab5a437c2eae4807112517a3474082a85e242 Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Tue, 10 Jan 2012 14:47:25 +0800 Subject: [PATCH] Switch touch queue to concurrent map - remove synchronize --- src/main/java/org/dynmap/MapManager.java | 47 ++++++++++--------- .../java/org/dynmap/utils/SnapshotCache.java | 4 +- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/dynmap/MapManager.java b/src/main/java/org/dynmap/MapManager.java index 26b0a8c9..b9dc4589 100644 --- a/src/main/java/org/dynmap/MapManager.java +++ b/src/main/java/org/dynmap/MapManager.java @@ -11,6 +11,7 @@ import java.util.List; import java.util.Map; import java.util.TreeSet; import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.RejectedExecutionException; @@ -87,19 +88,21 @@ public class MapManager { private static final int POOL_SIZE = 3; /* Touch event queues */ - private static class TouchEvent implements Comparable { + private static class TouchEvent { int x, y, z; String world; String reason; @Override - public int compareTo(TouchEvent te) { - if(x < te.x) return -1; - if(x > te.x) return 1; - if(y < te.y) return -1; - if(y > te.y) return 1; - if(z < te.z) return -1; - if(z > te.z) return 1; - return world.compareTo(te.world); + public int hashCode() { + return (x << 16) ^ (y << 24) ^ z; + } + @Override + public boolean equals(Object o) { + if(this == o) return true; + TouchEvent te = (TouchEvent)o; + if((x != te.x) || (y != te.y) || (z != te.z) || (world.equals(te.world) == false)) + return false; + return true; } } private static class TouchVolumeEvent { @@ -108,7 +111,7 @@ public class MapManager { String world; String reason; } - private TreeSet touch_events = new TreeSet(); + private ConcurrentHashMap touch_events = new ConcurrentHashMap(); private LinkedList touch_volume_events = new LinkedList(); private Object touch_lock = new Object(); @@ -1051,9 +1054,7 @@ public class MapManager { evt.y = y; evt.z = z; evt.reason = reason; - synchronized(touch_lock) { - touch_events.add(evt); - } + touch_events.putIfAbsent(evt, reason); } public void touchVolume(String wname, int minx, int miny, int minz, int maxx, int maxy, int maxz, String reason) { @@ -1410,16 +1411,20 @@ public class MapManager { * Process touch events */ private void processTouchEvents() { - TreeSet te = null; - LinkedList tve = null; - synchronized(touch_lock) { - if(touch_events.isEmpty() == false) { - te = touch_events; - touch_events = new TreeSet(); + ArrayList te = null; + ArrayList tve = null; + + if(touch_events.isEmpty() == false) { + te = new ArrayList(touch_events.keySet()); + for(int i = 0; i < te.size(); i++) { + touch_events.remove(te.get(i)); } + } + + synchronized(touch_lock) { if(touch_volume_events.isEmpty() == false) { - tve = touch_volume_events; - touch_volume_events = new LinkedList(); + tve = new ArrayList(touch_volume_events); + touch_volume_events.clear(); } } DynmapWorld world = null; diff --git a/src/main/java/org/dynmap/utils/SnapshotCache.java b/src/main/java/org/dynmap/utils/SnapshotCache.java index 50f928e2..8577932b 100644 --- a/src/main/java/org/dynmap/utils/SnapshotCache.java +++ b/src/main/java/org/dynmap/utils/SnapshotCache.java @@ -61,7 +61,7 @@ public class SnapshotCache { snapcache.reverselookup.remove(rec.ref); rec.ref.clear(); } - processRefQueue(); + //processRefQueue(); } /** * Invalidate cached snapshot, if in cache @@ -77,7 +77,7 @@ public class SnapshotCache { } } } - processRefQueue(); + //processRefQueue(); } /** * Look for chunk snapshot in cache