Scheduler uses linked list instead of set for events

This commit is contained in:
7u83 2018-12-17 22:05:14 +01:00
parent 918bc91e16
commit 998dad7e66
2 changed files with 34 additions and 13 deletions

View File

@ -45,6 +45,7 @@ import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import opensesim.gui.AssetPairEditor.NewJDialog;
import opensesim.gui.exchangeeditor.ExchangeListDialog;
import org.json.JSONArray;
import org.json.JSONObject;
@ -604,8 +605,18 @@ public class SeSimApplication extends javax.swing.JFrame {
// world.createTrader(cfg);
opensesim.world.scheduler.Scheduler s = godworld.getScheduler();
class MyEvent extends Event {
int count = 0;
MyEvent(){
}
}
class MyListener implements EventListener{
World world;
MyListener(World world){
this.world = world;
@ -613,8 +624,10 @@ public class SeSimApplication extends javax.swing.JFrame {
@Override
public long receive(Event event) {
System.out.println("Received an Event");
world.schedule(this, event, 2000);
MyEvent e = (MyEvent) event;
System.out.printf("Received an Event %d\n",e.count);
e.count++;
world.schedule(this, event, 1000);
return -1;
}
@ -628,7 +641,7 @@ public class SeSimApplication extends javax.swing.JFrame {
}
MyListener listener = new MyListener(godworld.getWorld());
Event arg = new Event();
MyEvent arg = new MyEvent();
s.startTimerTask(listener, arg, WIDTH);

View File

@ -57,7 +57,7 @@ public class Scheduler extends Thread {
return this.acceleration;
}
private final SortedMap<Long, SortedSet<TimerTaskDef>> event_queue = new TreeMap<>();
private final SortedMap<Long, LinkedList<TimerTaskDef>> event_queue = new TreeMap<>();
@ -233,9 +233,9 @@ public class Scheduler extends Thread {
// System.out.printf("Add TimerTask %d %d\n",e.curevtime,e.newevtime);
// long evtime = time + currentTimeMillis();
SortedSet<TimerTaskDef> s = event_queue.get(e.newevtime);
LinkedList<TimerTaskDef> s = event_queue.get(e.newevtime);
if (s == null) {
s = new TreeSet<>();
s = new LinkedList<>();
event_queue.put(e.newevtime, s);
}
@ -257,7 +257,7 @@ public class Scheduler extends Thread {
// if (evtime == null) {
// return;
// }
SortedSet<TimerTaskDef> s = event_queue.get(e.curevtime);
LinkedList<TimerTaskDef> s = event_queue.get(e.curevtime);
if (s == null) {
// System.out.printf("My not found\n");
return;
@ -299,13 +299,21 @@ public class Scheduler extends Thread {
}
// if (t <= ct) {
SortedSet s = event_queue.get(t);
Object rc = event_queue.remove(t);
LinkedList<TimerTaskDef> s = event_queue.get(t);
Object rc;
rc = event_queue.remove(t);
if (s.size() > 1) {
while (s.size() > 0) {
TimerTaskDef def = s.pop();
long next_t = this.fireEvent(def.listener,def.arg);
if (next_t == -1)
continue;
def.newevtime = next_t + t;
this.addTimerTask(def);
//System.out.printf("Events in a row: %d\n", s.size());
}
/*
Iterator<TimerTaskDef> it = s.iterator();
while (it.hasNext()) {
TimerTaskDef def = it.next();
@ -318,7 +326,7 @@ public class Scheduler extends Thread {
def.newevtime = next_t + t;
this.addTimerTask(def);
}
return 0;
*/ return 0;
}