From 2c34038912b4c865034aa3109c426c5aa23f2a08 Mon Sep 17 00:00:00 2001 From: orbiter Date: Sun, 30 Mar 2008 21:17:12 +0000 Subject: [PATCH] addition/correction to last commit: usage of concurrent-classes git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4626 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- source/de/anomic/server/serverProfiling.java | 56 +++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/source/de/anomic/server/serverProfiling.java b/source/de/anomic/server/serverProfiling.java index a2cf445e1..d0e2dc292 100644 --- a/source/de/anomic/server/serverProfiling.java +++ b/source/de/anomic/server/serverProfiling.java @@ -28,18 +28,18 @@ package de.anomic.server; import java.util.Iterator; import java.util.Map; -import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; public class serverProfiling extends Thread { - private static Map> historyMaps; // key=name of history, value=TreeMap of Long/Event + private static Map> historyMaps; // key=name of history, value=TreeMap of Long/Event private static Map eventCounter; // key=name of history, value=Integer of event counter private static serverProfiling systemProfiler; static { // initialize profiling - historyMaps = new ConcurrentHashMap>(); + historyMaps = new ConcurrentHashMap>(); eventCounter = new ConcurrentHashMap(); //lastCompleteCleanup = System.currentTimeMillis(); systemProfiler = null; @@ -76,33 +76,37 @@ public class serverProfiling extends Thread { public static void update(String eventName, Object eventPayload) { // get event history container int counter = eventCounter.containsKey(eventName) ? ((Integer) eventCounter.get(eventName)).intValue() : 0; - TreeMap history = historyMaps.containsKey(eventName) ? (historyMaps.get(eventName)) : new TreeMap(); + if (historyMaps.containsKey(eventName)) { + ConcurrentLinkedQueue history = historyMaps.get(eventName); - // update entry - Long time = new Long(System.currentTimeMillis()); - history.put(time, new Event(counter, eventPayload)); - counter++; - eventCounter.put(eventName, new Integer(counter)); - - // clean up too old entries - cleanup(history); - - // store map - historyMaps.put(eventName, history); - } - - private static void cleanup(TreeMap history) { - // clean up too old entries - while (history.size() > 0) { - Long time = history.firstKey(); - if (System.currentTimeMillis() - time.longValue() < 600000) break; - history.remove(time); - } - + // update entry + history.add(new Event(counter, eventPayload)); + counter++; + eventCounter.put(eventName, new Integer(counter)); + + // clean up too old entries + Event e; + long now = System.currentTimeMillis(); + while (history.size() > 0) { + e = history.peek(); + if (now - e.time < 600000) break; + history.poll(); + } + } else { + ConcurrentLinkedQueue history = new ConcurrentLinkedQueue(); + + // update entry + history.add(new Event(counter, eventPayload)); + counter++; + eventCounter.put(eventName, new Integer(counter)); + + // store map + historyMaps.put(eventName, history); + } } public static Iterator history(String eventName) { - return (historyMaps.containsKey(eventName) ? (historyMaps.get(eventName)) : new TreeMap()).values().iterator(); + return (historyMaps.containsKey(eventName) ? (historyMaps.get(eventName)) : new ConcurrentLinkedQueue()).iterator(); } public static class Event {