Scheduler uses linked list instead of set for events
This commit is contained in:
parent
918bc91e16
commit
998dad7e66
@ -45,6 +45,7 @@ import javax.swing.JOptionPane;
|
|||||||
import javax.swing.filechooser.FileFilter;
|
import javax.swing.filechooser.FileFilter;
|
||||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||||
import opensesim.gui.AssetPairEditor.NewJDialog;
|
import opensesim.gui.AssetPairEditor.NewJDialog;
|
||||||
|
|
||||||
import opensesim.gui.exchangeeditor.ExchangeListDialog;
|
import opensesim.gui.exchangeeditor.ExchangeListDialog;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
@ -604,8 +605,18 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
// world.createTrader(cfg);
|
// world.createTrader(cfg);
|
||||||
|
|
||||||
opensesim.world.scheduler.Scheduler s = godworld.getScheduler();
|
opensesim.world.scheduler.Scheduler s = godworld.getScheduler();
|
||||||
|
|
||||||
|
class MyEvent extends Event {
|
||||||
|
int count = 0;
|
||||||
|
MyEvent(){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
class MyListener implements EventListener{
|
class MyListener implements EventListener{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
World world;
|
World world;
|
||||||
MyListener(World world){
|
MyListener(World world){
|
||||||
this.world = world;
|
this.world = world;
|
||||||
@ -613,8 +624,10 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long receive(Event event) {
|
public long receive(Event event) {
|
||||||
System.out.println("Received an Event");
|
MyEvent e = (MyEvent) event;
|
||||||
world.schedule(this, event, 2000);
|
System.out.printf("Received an Event %d\n",e.count);
|
||||||
|
e.count++;
|
||||||
|
world.schedule(this, event, 1000);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -628,7 +641,7 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
MyListener listener = new MyListener(godworld.getWorld());
|
MyListener listener = new MyListener(godworld.getWorld());
|
||||||
Event arg = new Event();
|
MyEvent arg = new MyEvent();
|
||||||
|
|
||||||
s.startTimerTask(listener, arg, WIDTH);
|
s.startTimerTask(listener, arg, WIDTH);
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ public class Scheduler extends Thread {
|
|||||||
return this.acceleration;
|
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);
|
// System.out.printf("Add TimerTask %d %d\n",e.curevtime,e.newevtime);
|
||||||
// long evtime = time + currentTimeMillis();
|
// long evtime = time + currentTimeMillis();
|
||||||
SortedSet<TimerTaskDef> s = event_queue.get(e.newevtime);
|
LinkedList<TimerTaskDef> s = event_queue.get(e.newevtime);
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
s = new TreeSet<>();
|
s = new LinkedList<>();
|
||||||
event_queue.put(e.newevtime, s);
|
event_queue.put(e.newevtime, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -257,7 +257,7 @@ public class Scheduler extends Thread {
|
|||||||
// if (evtime == null) {
|
// if (evtime == null) {
|
||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
SortedSet<TimerTaskDef> s = event_queue.get(e.curevtime);
|
LinkedList<TimerTaskDef> s = event_queue.get(e.curevtime);
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
// System.out.printf("My not found\n");
|
// System.out.printf("My not found\n");
|
||||||
return;
|
return;
|
||||||
@ -299,13 +299,21 @@ public class Scheduler extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if (t <= ct) {
|
// if (t <= ct) {
|
||||||
SortedSet s = event_queue.get(t);
|
LinkedList<TimerTaskDef> s = event_queue.get(t);
|
||||||
Object rc = event_queue.remove(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());
|
//System.out.printf("Events in a row: %d\n", s.size());
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
Iterator<TimerTaskDef> it = s.iterator();
|
Iterator<TimerTaskDef> it = s.iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
TimerTaskDef def = it.next();
|
TimerTaskDef def = it.next();
|
||||||
@ -318,7 +326,7 @@ public class Scheduler extends Thread {
|
|||||||
def.newevtime = next_t + t;
|
def.newevtime = next_t + t;
|
||||||
this.addTimerTask(def);
|
this.addTimerTask(def);
|
||||||
}
|
}
|
||||||
return 0;
|
*/ return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user