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
pull/1/head
orbiter 17 years ago
parent b2150057d2
commit 2c34038912

@ -28,18 +28,18 @@ package de.anomic.server;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
public class serverProfiling extends Thread { public class serverProfiling extends Thread {
private static Map<String, TreeMap<Long, Event>> historyMaps; // key=name of history, value=TreeMap of Long/Event private static Map<String, ConcurrentLinkedQueue<Event>> historyMaps; // key=name of history, value=TreeMap of Long/Event
private static Map<String, Integer> eventCounter; // key=name of history, value=Integer of event counter private static Map<String, Integer> eventCounter; // key=name of history, value=Integer of event counter
private static serverProfiling systemProfiler; private static serverProfiling systemProfiler;
static { static {
// initialize profiling // initialize profiling
historyMaps = new ConcurrentHashMap<String, TreeMap<Long, Event>>(); historyMaps = new ConcurrentHashMap<String, ConcurrentLinkedQueue<Event>>();
eventCounter = new ConcurrentHashMap<String, Integer>(); eventCounter = new ConcurrentHashMap<String, Integer>();
//lastCompleteCleanup = System.currentTimeMillis(); //lastCompleteCleanup = System.currentTimeMillis();
systemProfiler = null; systemProfiler = null;
@ -76,33 +76,37 @@ public class serverProfiling extends Thread {
public static void update(String eventName, Object eventPayload) { public static void update(String eventName, Object eventPayload) {
// get event history container // get event history container
int counter = eventCounter.containsKey(eventName) ? ((Integer) eventCounter.get(eventName)).intValue() : 0; int counter = eventCounter.containsKey(eventName) ? ((Integer) eventCounter.get(eventName)).intValue() : 0;
TreeMap<Long, Event> history = historyMaps.containsKey(eventName) ? (historyMaps.get(eventName)) : new TreeMap<Long, Event>(); if (historyMaps.containsKey(eventName)) {
ConcurrentLinkedQueue<Event> history = historyMaps.get(eventName);
// update entry // update entry
Long time = new Long(System.currentTimeMillis()); history.add(new Event(counter, eventPayload));
history.put(time, new Event(counter, eventPayload));
counter++; counter++;
eventCounter.put(eventName, new Integer(counter)); eventCounter.put(eventName, new Integer(counter));
// clean up too old entries // clean up too old entries
cleanup(history); Event e;
long now = System.currentTimeMillis();
while (history.size() > 0) {
e = history.peek();
if (now - e.time < 600000) break;
history.poll();
}
} else {
ConcurrentLinkedQueue<Event> history = new ConcurrentLinkedQueue<Event>();
// update entry
history.add(new Event(counter, eventPayload));
counter++;
eventCounter.put(eventName, new Integer(counter));
// store map // store map
historyMaps.put(eventName, history); historyMaps.put(eventName, history);
} }
private static void cleanup(TreeMap<Long, Event> history) {
// clean up too old entries
while (history.size() > 0) {
Long time = history.firstKey();
if (System.currentTimeMillis() - time.longValue() < 600000) break;
history.remove(time);
}
} }
public static Iterator<Event> history(String eventName) { public static Iterator<Event> history(String eventName) {
return (historyMaps.containsKey(eventName) ? (historyMaps.get(eventName)) : new TreeMap<Long, Event>()).values().iterator(); return (historyMaps.containsKey(eventName) ? (historyMaps.get(eventName)) : new ConcurrentLinkedQueue<Event>()).iterator();
} }
public static class Event { public static class Event {

Loading…
Cancel
Save