Fine scheduler
This commit is contained in:
parent
ad7ba1be01
commit
2e269b42d8
@ -1,4 +1,4 @@
|
||||
#Mon, 17 Dec 2018 23:08:48 +0100
|
||||
#Tue, 18 Dec 2018 19:45:44 +0100
|
||||
annotation.processing.enabled=true
|
||||
annotation.processing.enabled.in.editor=false
|
||||
annotation.processing.processors.list=
|
||||
|
@ -56,9 +56,10 @@ import opensesim.old_sesim.Scheduler;
|
||||
import opensesim.old_sesim.Scheduler.TimerTaskDef;
|
||||
|
||||
import opensesim.world.GodWorld;
|
||||
import opensesim.world.Trader;
|
||||
import opensesim.world.World;
|
||||
|
||||
import opensesim.world.scheduler.Scheduler.EventListener;
|
||||
import opensesim.world.scheduler.EventListener;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -599,7 +600,8 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
JSONObject cfg = new JSONObject("{"
|
||||
+ "strategy: opensesim.trader.SimpleTrader"
|
||||
+ "}");
|
||||
// world.createTrader(cfg);
|
||||
Trader t = godworld.createTrader(cfg);
|
||||
t.start();
|
||||
|
||||
opensesim.world.scheduler.Scheduler s = godworld.getScheduler();
|
||||
|
||||
@ -616,8 +618,8 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
|
||||
@Override
|
||||
public long receive(opensesim.world.scheduler.Event task) {
|
||||
|
||||
System.out.printf("Received an Event %d\n", 0);
|
||||
|
||||
System.out.printf("Received an Event %d\n", Thread.currentThread().getId());
|
||||
// e.count++;
|
||||
world.schedule(this, 1000);
|
||||
return -1;
|
||||
|
@ -27,13 +27,15 @@ package opensesim.trader;
|
||||
|
||||
import opensesim.world.AbstractTrader;
|
||||
import opensesim.world.World;
|
||||
import opensesim.world.scheduler.Event;
|
||||
import opensesim.world.scheduler.EventListener;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class SimpleTrader extends AbstractTrader{
|
||||
public class SimpleTrader extends AbstractTrader implements EventListener{
|
||||
|
||||
@Override
|
||||
public String getStrategyTypeName() {
|
||||
@ -41,14 +43,51 @@ public class SimpleTrader extends AbstractTrader{
|
||||
}
|
||||
|
||||
|
||||
SimpleTrader(World world, JSONObject cfg){
|
||||
public SimpleTrader(World world, JSONObject cfg){
|
||||
super(world,cfg);
|
||||
}
|
||||
|
||||
SimpleTrader(){
|
||||
public SimpleTrader(){
|
||||
this(null,null);
|
||||
}
|
||||
|
||||
float initial_delay[] = new float[2];
|
||||
|
||||
/**
|
||||
* Get a (long) random number between min an max
|
||||
*
|
||||
* @param min minimum value
|
||||
* @param max maximeum value
|
||||
* @return the number
|
||||
*/
|
||||
protected float getRandom(float min, float max) {
|
||||
//double r = se.randNextDouble();
|
||||
|
||||
// System.out.printf("RD: %f", r);
|
||||
// System.exit(0);
|
||||
// return (max - min) * r + min;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
long delay = (long) (1000.0f * getWorld().randNextFloat(3.0f, 12.7f));
|
||||
System.out.printf("Initial delay %d\n", delay);
|
||||
|
||||
getWorld().schedule(this, delay);
|
||||
|
||||
// long delay = (long) (getRandom(initial_delay[0], initial_delay[1]) * 1000);
|
||||
// setStatus("Inital delay: %d", delay);
|
||||
// timerTask = se.timer.startTimerTask(this, delay);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long receive(Event task) {
|
||||
System.out.printf("Here we are !!! %f\n", getWorld().randNextFloat(12f, 27f));
|
||||
getWorld().schedule(this, 100);
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -92,12 +92,6 @@ public abstract class AbstractTrader implements Trader {
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
/* long delay = (long) (getRandom(initial_delay[0], initial_delay[1]) * 1000);
|
||||
setStatus("Inital delay: %d", delay);
|
||||
timerTask = se.timer.startTimerTask(this, delay);*/
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -32,14 +32,17 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Random;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import opensesim.sesim.interfaces.GetJson;
|
||||
import opensesim.util.Scollection;
|
||||
import opensesim.util.SeSimException;
|
||||
import opensesim.util.idgenerator.IDGenerator;
|
||||
import opensesim.world.scheduler.EventListener;
|
||||
|
||||
import opensesim.world.scheduler.Scheduler;
|
||||
import opensesim.world.scheduler.StScheduler;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
@ -51,7 +54,7 @@ import org.json.JSONObject;
|
||||
public class GodWorld implements GetJson, World {
|
||||
|
||||
@Override
|
||||
public void schedule(Scheduler.EventListener listener, long t) {
|
||||
public void schedule(EventListener listener, long t) {
|
||||
scheduler.startTimerTask(listener, t);
|
||||
}
|
||||
|
||||
@ -84,8 +87,7 @@ public class GodWorld implements GetJson, World {
|
||||
* @param cfg
|
||||
*/
|
||||
public GodWorld(JSONObject cfg) {
|
||||
init(cfg,false);
|
||||
|
||||
init(cfg, false);
|
||||
|
||||
}
|
||||
|
||||
@ -342,13 +344,13 @@ public class GodWorld implements GetJson, World {
|
||||
Class cls;
|
||||
try {
|
||||
cls = (Class<Trader>) Class.forName(strategy);
|
||||
trader = (AbstractTrader) cls.getConstructor(World.class, JSONObject.class).newInstance(null, cfg);
|
||||
trader = (AbstractTrader) cls.getConstructor(World.class, JSONObject.class).newInstance(this.getWorld(), cfg);
|
||||
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
|
||||
Logger.getLogger(GodWorld.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
return trader;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -359,4 +361,37 @@ public class GodWorld implements GetJson, World {
|
||||
// --------------------------------------------------------------------
|
||||
// Stuff belonging to accounts
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Pseudo random generator stuff
|
||||
// --------------------------------------------------------------------
|
||||
Random random = new Random(34561);
|
||||
|
||||
public int randNextInt() {
|
||||
return random.nextInt();
|
||||
}
|
||||
|
||||
public int randNextInt(int bounds) {
|
||||
return random.nextInt(bounds);
|
||||
}
|
||||
|
||||
public double randNextDouble() {
|
||||
return random.nextDouble();
|
||||
}
|
||||
|
||||
public float randNextFloat() {
|
||||
return random.nextFloat();
|
||||
}
|
||||
|
||||
public boolean randNextBool() {
|
||||
return random.nextBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float randNextFloat(float min, float max) {
|
||||
float r = randNextFloat();
|
||||
return (max - min) * r + min;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,18 +26,20 @@
|
||||
package opensesim.world;
|
||||
|
||||
import java.util.Collection;
|
||||
import opensesim.world.scheduler.EventListener;
|
||||
|
||||
import opensesim.world.scheduler.Scheduler;
|
||||
import opensesim.world.scheduler.StScheduler;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class RealWorld implements World{
|
||||
public class RealWorld implements World {
|
||||
|
||||
private GodWorld godworld;
|
||||
|
||||
protected RealWorld(GodWorld godworld){
|
||||
this.godworld=godworld;
|
||||
|
||||
protected RealWorld(GodWorld godworld) {
|
||||
this.godworld = godworld;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -61,8 +63,13 @@ public class RealWorld implements World{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void schedule(Scheduler.EventListener listener, long t) {
|
||||
godworld.schedule(listener, t);
|
||||
public void schedule(EventListener listener, long t) {
|
||||
godworld.schedule(listener, t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float randNextFloat(float min, float max) {
|
||||
return godworld.randNextFloat(min, max);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ package opensesim.world;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import opensesim.world.scheduler.Scheduler.EventListener;
|
||||
import opensesim.world.scheduler.EventListener;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -42,6 +42,8 @@ public interface World {
|
||||
Collection<Exchange> getExchangeCollection();
|
||||
|
||||
Collection<Trader> getTradersCollection();
|
||||
|
||||
public void schedule(EventListener listener, long t);
|
||||
|
||||
public void schedule(EventListener listener, long t);
|
||||
|
||||
public float randNextFloat(float min, float max);
|
||||
}
|
||||
|
107
src/opensesim/world/scheduler/Clock.java
Normal file
107
src/opensesim/world/scheduler/Clock.java
Normal file
@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (c) 2018, tohe
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package opensesim.world.scheduler;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class Clock {
|
||||
|
||||
|
||||
private double current_millis = 0.0;
|
||||
private long last_nanos = System.nanoTime();
|
||||
private double current_nanos = 0;
|
||||
private double acceleration = 1.0;
|
||||
|
||||
public double getAcceleration() {
|
||||
return acceleration;
|
||||
}
|
||||
|
||||
public void setAcceleration(double acceleration) {
|
||||
this.acceleration = acceleration;
|
||||
}
|
||||
private boolean pause = false;
|
||||
|
||||
public boolean isPause() {
|
||||
return pause;
|
||||
}
|
||||
|
||||
public void setPause(boolean pause) {
|
||||
this.pause = pause;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private long currentTimeMillis1() {
|
||||
|
||||
long cur = System.nanoTime();
|
||||
long diff = cur - last_nanos;
|
||||
last_nanos = cur;
|
||||
|
||||
if (pause) {
|
||||
return (long) this.current_millis;
|
||||
}
|
||||
|
||||
// this.cur_nano += (((double)diff_nano)/1000000.0)*this.acceleration;
|
||||
// return (long)(cur_nano/1000000.0);
|
||||
this.current_nanos += (double) diff * (double) this.acceleration;
|
||||
|
||||
// this.current_time_millis += ((double) diff) * this.acceleration;
|
||||
this.current_millis = this.current_nanos / 1000000.0;
|
||||
|
||||
return (long) this.current_millis;
|
||||
}
|
||||
|
||||
public long currentTimeMillis() {
|
||||
return (long) this.current_millis;
|
||||
}
|
||||
|
||||
synchronized long getDelay(long t) {
|
||||
long ct = currentTimeMillis1();
|
||||
|
||||
// ct = t;
|
||||
if (t > ct) {
|
||||
//if ((long) diff > 0) {
|
||||
// System.out.printf("Leave Event Queue in run events %d\n", Thread.currentThread().getId());
|
||||
// System.out.printf("Sleeping somewat %d\n", (long) (0.5 + (t - this.currentTimeMillis()) / this.acceleration));
|
||||
// return (long) diff;
|
||||
return (long) (((double) t - this.current_millis) / this.acceleration);
|
||||
}
|
||||
|
||||
if (t < ct) {
|
||||
// System.out.printf("Time is overslipping: %d\n",ct-t);
|
||||
this.current_millis = t;
|
||||
this.current_nanos = this.current_millis * 1000000.0;
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
@ -25,7 +25,6 @@
|
||||
*/
|
||||
package opensesim.world.scheduler;
|
||||
|
||||
import opensesim.world.scheduler.Scheduler.EventListener;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -35,22 +34,8 @@ import opensesim.world.scheduler.Scheduler.EventListener;
|
||||
|
||||
EventListener listener;
|
||||
|
||||
|
||||
long curevtime;
|
||||
long newevtime;
|
||||
// int id;
|
||||
|
||||
Event(EventListener listener, long t) {
|
||||
Event(EventListener listener) {
|
||||
this.listener = listener;
|
||||
|
||||
newevtime = t;
|
||||
// id = nextTimerTask.getAndAdd(1);
|
||||
|
||||
}
|
||||
|
||||
/* @Override
|
||||
public int compareTo(Object o) {
|
||||
return ((TimerTaskDef) o).id - this.id;
|
||||
}
|
||||
*/
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2018, 7u83 <7u83@mail.ru>
|
||||
* Copyright (c) 2018, 7u83
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -25,377 +25,41 @@
|
||||
*/
|
||||
package opensesim.world.scheduler;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.ArrayList;
|
||||
import opensesim.world.scheduler.EventListener;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
* @author tohe
|
||||
*/
|
||||
public class Scheduler extends Thread {
|
||||
|
||||
private double acceleration = 1.0;
|
||||
|
||||
public void setAcceleration(double acceleration) {
|
||||
|
||||
this.acceleration = acceleration;
|
||||
synchronized (this) {
|
||||
this.notify();
|
||||
}
|
||||
}
|
||||
|
||||
public double getAcceleration() {
|
||||
|
||||
return this.acceleration;
|
||||
}
|
||||
|
||||
private final SortedMap<Long, LinkedList<Event>> event_queue = new TreeMap<>();
|
||||
public class Scheduler {
|
||||
ArrayList<StScheduler> schedulers;
|
||||
Clock clock = new Clock();
|
||||
int next=0;
|
||||
|
||||
public Scheduler(int nthreads){
|
||||
schedulers = new ArrayList<>();
|
||||
for (int i=0; i<nthreads; i++){
|
||||
schedulers.add(i,new StScheduler(clock));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public interface EventListener {
|
||||
|
||||
long receive(Event task);
|
||||
|
||||
// long getID();
|
||||
}
|
||||
|
||||
private boolean terminate = false;
|
||||
|
||||
/**
|
||||
* Terminate the scheduler
|
||||
*/
|
||||
public void terminate() {
|
||||
terminate = true;
|
||||
synchronized (event_queue) {
|
||||
event_queue.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
if (this.isAlive()) {
|
||||
// thread is already running
|
||||
return;
|
||||
}
|
||||
initScheduler();
|
||||
super.start();
|
||||
}
|
||||
|
||||
/* private class ObjectComparator implements Comparator<Object> {
|
||||
|
||||
@Override
|
||||
public int compare(Object o1, Object o2) {
|
||||
|
||||
return (((EventListener) o1).getID() - ((EventListener) o2).getID()) < 0 ? -1 : 1;
|
||||
//return System.identityHashCode(o1) - System.identityHashCode(o2);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
long last_time_millis = System.currentTimeMillis();
|
||||
double current_time_millis = 0.0;
|
||||
|
||||
long last_nanos = System.nanoTime();
|
||||
double current_nanos = 0;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private long currentTimeMillis1() {
|
||||
|
||||
long cur = System.nanoTime();
|
||||
long diff = cur - last_nanos;
|
||||
last_nanos = cur;
|
||||
|
||||
if (pause) {
|
||||
return (long) this.current_time_millis;
|
||||
}
|
||||
|
||||
// this.cur_nano += (((double)diff_nano)/1000000.0)*this.acceleration;
|
||||
// return (long)(cur_nano/1000000.0);
|
||||
this.current_nanos += (double) diff * (double) this.acceleration;
|
||||
|
||||
// this.current_time_millis += ((double) diff) * this.acceleration;
|
||||
this.current_time_millis = this.current_nanos / 1000000.0;
|
||||
|
||||
return (long) this.current_time_millis;
|
||||
}
|
||||
|
||||
public long currentTimeMillis() {
|
||||
return (long) this.current_time_millis;
|
||||
}
|
||||
|
||||
static public String formatTimeMillis(long t) {
|
||||
Date date = new Date(t);
|
||||
// DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
// String dateFormatted = formatter.format(date);
|
||||
// return dateFormatted;
|
||||
long seconds = (t / 1000) % 60;
|
||||
long minutes = (t / 1000 / 60) % 60;
|
||||
long hours = (t / 1000) / (60 * 60);
|
||||
|
||||
return String.format("%d:%02d:%02d", hours, minutes, seconds);
|
||||
}
|
||||
|
||||
AtomicInteger nextTimerTask = new AtomicInteger(0);
|
||||
|
||||
/* public class TimerTaskDef {
|
||||
|
||||
EventListener listener;
|
||||
Event arg;
|
||||
|
||||
long curevtime;
|
||||
long newevtime;
|
||||
int id;
|
||||
|
||||
TimerTaskDef(EventListener listener, Event arg, long t) {
|
||||
this.listener = listener;
|
||||
this.arg=arg;
|
||||
newevtime = t;
|
||||
id = nextTimerTask.getAndAdd(1);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
//LinkedList<TimerTaskDef> set_tasks = new LinkedList<>();
|
||||
ConcurrentLinkedQueue<Event> new_tasks = new ConcurrentLinkedQueue<>();
|
||||
|
||||
/**
|
||||
*
|
||||
* @param listener
|
||||
* @param time
|
||||
* @return The TimerTask created
|
||||
*/
|
||||
public Event startTimerTask(EventListener listener, long time) {
|
||||
|
||||
long evtime = time + currentTimeMillis();
|
||||
|
||||
Event task = new Event(listener, evtime);
|
||||
new_tasks.add(task);
|
||||
|
||||
synchronized (this) {
|
||||
notify();
|
||||
}
|
||||
return task;
|
||||
public Scheduler(){
|
||||
this(1);
|
||||
}
|
||||
|
||||
|
||||
public Event scheduleEvent(Event e){
|
||||
new_tasks.add(e);
|
||||
synchronized (this) {
|
||||
notify();
|
||||
}
|
||||
public void start(){
|
||||
for (StScheduler s: schedulers){
|
||||
s.start();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized Event startTimerTask(EventListener listener, long time) {
|
||||
Event e = schedulers.get(next++).startTimerTask(listener, time);
|
||||
if (next==schedulers.size())
|
||||
next=0;
|
||||
return e;
|
||||
}
|
||||
|
||||
public void rescheduleTimerTask(Event task, long time) {
|
||||
long evtime = time + currentTimeMillis();
|
||||
task.newevtime = evtime;
|
||||
new_tasks.add(task);
|
||||
|
||||
synchronized (this) {
|
||||
notify();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean pause = false;
|
||||
|
||||
public void pause() {
|
||||
setPause(!pause);
|
||||
|
||||
}
|
||||
|
||||
public void setPause(boolean val) {
|
||||
pause = val;
|
||||
synchronized (this) {
|
||||
this.notify();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean getPause() {
|
||||
return pause;
|
||||
}
|
||||
|
||||
public long fireEvent(EventListener e, Event arg) {
|
||||
return e.receive(arg);// .receive(e,arg);
|
||||
}
|
||||
|
||||
// HashMap<TimerTaskDef, Long> tasks = new HashMap<>();
|
||||
private boolean addTimerTask(Event e) {
|
||||
|
||||
// System.out.printf("Add TimerTask %d %d\n",e.curevtime,e.newevtime);
|
||||
// long evtime = time + currentTimeMillis();
|
||||
LinkedList<Event> s = event_queue.get(e.newevtime);
|
||||
if (s == null) {
|
||||
s = new LinkedList<>();
|
||||
event_queue.put(e.newevtime, s);
|
||||
}
|
||||
|
||||
e.curevtime = e.newevtime;
|
||||
|
||||
// tasks.put(e, e.evtime);
|
||||
return s.add(e);
|
||||
}
|
||||
|
||||
private final LinkedList<EventListener> cancel_queue = new LinkedList();
|
||||
|
||||
public void cancelTimerTask(EventListener e) {
|
||||
cancel_queue.add(e);
|
||||
}
|
||||
|
||||
private void cancelMy(Event e) {
|
||||
|
||||
// Long evtime = tasks.get(e.curevtime);
|
||||
// if (evtime == null) {
|
||||
// return;
|
||||
// }
|
||||
LinkedList<Event> s = event_queue.get(e.curevtime);
|
||||
if (s == null) {
|
||||
// System.out.printf("My not found\n");
|
||||
return;
|
||||
}
|
||||
|
||||
Boolean rc = s.remove(e);
|
||||
|
||||
if (s.isEmpty()) {
|
||||
|
||||
event_queue.remove(e.curevtime);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public long runEvents() {
|
||||
synchronized (event_queue) {
|
||||
|
||||
if (event_queue.isEmpty()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
long t = event_queue.firstKey();
|
||||
long ct = currentTimeMillis1();
|
||||
|
||||
// ct = t;
|
||||
if (t > ct) {
|
||||
//if ((long) diff > 0) {
|
||||
// System.out.printf("Leave Event Queue in run events %d\n", Thread.currentThread().getId());
|
||||
// System.out.printf("Sleeping somewat %d\n", (long) (0.5 + (t - this.currentTimeMillis()) / this.acceleration));
|
||||
// return (long) diff;
|
||||
return (long) (((double) t - this.currentTimeMillis()) / this.acceleration);
|
||||
}
|
||||
|
||||
if (t < ct) {
|
||||
// System.out.printf("Time is overslipping: %d\n",ct-t);
|
||||
this.current_time_millis = t;
|
||||
this.current_nanos = this.current_time_millis * 1000000.0;
|
||||
|
||||
}
|
||||
|
||||
// if (t <= ct) {
|
||||
LinkedList<Event> s = event_queue.get(t);
|
||||
Object rc;
|
||||
rc = event_queue.remove(t);
|
||||
|
||||
while (s.size() > 0) {
|
||||
Event def = s.pop();
|
||||
long next_t = this.fireEvent(def.listener,def);
|
||||
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();
|
||||
|
||||
|
||||
long next_t = this.fireEvent(def.listener,def.arg);
|
||||
if (next_t == -1)
|
||||
continue;
|
||||
|
||||
def.newevtime = next_t + t;
|
||||
this.addTimerTask(def);
|
||||
}
|
||||
*/ return 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
class EmptyCtr implements EventListener {
|
||||
|
||||
@Override
|
||||
public long receive() {
|
||||
// System.out.printf("Current best brice %f\n", Globals.se.getBestPrice());
|
||||
return 1000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getID() {
|
||||
return 999999999999999999L;
|
||||
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
void initScheduler() {
|
||||
current_time_millis = 0.0;
|
||||
// this.startTimerTask(new EmptyCtr(), 0);
|
||||
terminate = false;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
while (!terminate) {
|
||||
|
||||
while (!new_tasks.isEmpty()) {
|
||||
Event td = new_tasks.poll();
|
||||
// System.out.printf("There is a set task %d %d\n",td.curevtime,td.newevtime);
|
||||
|
||||
this.cancelMy(td);
|
||||
this.addTimerTask(td);
|
||||
|
||||
}
|
||||
|
||||
long wtime = runEvents();
|
||||
|
||||
if (wtime == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
synchronized (this) {
|
||||
try {
|
||||
// System.out.printf("My WTIME %d\n", wtime);
|
||||
if (wtime != -1 && !pause) {
|
||||
wait(wtime);
|
||||
} else {
|
||||
wait();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.event_queue.clear();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ import java.util.LinkedList;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
@ -156,7 +156,16 @@ public class StScheduler extends Thread {
|
||||
*/
|
||||
|
||||
//LinkedList<TimerTaskDef> set_tasks = new LinkedList<>();
|
||||
ConcurrentLinkedQueue<Event> new_tasks = new ConcurrentLinkedQueue<>();
|
||||
|
||||
class NewEvent {
|
||||
Event e;
|
||||
long t;
|
||||
NewEvent(Event e, long t){
|
||||
this.e=e;
|
||||
this.t=t;
|
||||
}
|
||||
}
|
||||
ConcurrentLinkedQueue<NewEvent> new_tasks = new ConcurrentLinkedQueue<>();
|
||||
|
||||
/**
|
||||
*
|
||||
@ -168,8 +177,8 @@ public class StScheduler extends Thread {
|
||||
|
||||
long evtime = time + clock.currentTimeMillis();
|
||||
|
||||
Event task = new Event(listener, evtime);
|
||||
new_tasks.add(task);
|
||||
Event task = new Event(listener);
|
||||
new_tasks.add(new NewEvent(task,evtime));
|
||||
|
||||
synchronized (this) {
|
||||
notify();
|
||||
@ -178,59 +187,46 @@ public class StScheduler extends Thread {
|
||||
}
|
||||
|
||||
|
||||
public Event scheduleEvent(Event e){
|
||||
public Event scheduleEvent(NewEvent e){
|
||||
new_tasks.add(e);
|
||||
synchronized (this) {
|
||||
notify();
|
||||
}
|
||||
return e;
|
||||
return e.e;
|
||||
}
|
||||
|
||||
public void rescheduleTimerTask(Event task, long time) {
|
||||
public void rescheduleTimerTask(Event e, long time) {
|
||||
long evtime = time + clock.currentTimeMillis();
|
||||
task.newevtime = evtime;
|
||||
new_tasks.add(task);
|
||||
// task.newevtime = evtime;
|
||||
new_tasks.add(new NewEvent(e,evtime));
|
||||
|
||||
synchronized (this) {
|
||||
notify();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean pause = false;
|
||||
// private boolean pause = false;
|
||||
|
||||
public void pause() {
|
||||
setPause(!pause);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void setPause(boolean val) {
|
||||
pause = val;
|
||||
synchronized (this) {
|
||||
this.notify();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean getPause() {
|
||||
return pause;
|
||||
}
|
||||
|
||||
public long fireEvent(EventListener e, Event arg) {
|
||||
return e.receive(arg);// .receive(e,arg);
|
||||
}
|
||||
|
||||
// HashMap<TimerTaskDef, Long> tasks = new HashMap<>();
|
||||
private boolean addTimerTask(Event e) {
|
||||
private boolean addTimerTask(Event e, long t) {
|
||||
|
||||
// System.out.printf("Add TimerTask %d %d\n",e.curevtime,e.newevtime);
|
||||
// long evtime = time + currentTimeMillis();
|
||||
LinkedList<Event> s = event_queue.get(e.newevtime);
|
||||
LinkedList<Event> s = event_queue.get(t);
|
||||
if (s == null) {
|
||||
s = new LinkedList<>();
|
||||
event_queue.put(e.newevtime, s);
|
||||
event_queue.put(t, s);
|
||||
}
|
||||
|
||||
e.curevtime = e.newevtime;
|
||||
//e.curevtime = e.newevtime;
|
||||
|
||||
// tasks.put(e, e.evtime);
|
||||
return s.add(e);
|
||||
@ -242,7 +238,7 @@ public class StScheduler extends Thread {
|
||||
cancel_queue.add(e);
|
||||
}
|
||||
|
||||
private void cancelMy(Event e) {
|
||||
/* private void cancelMy(Event e) {
|
||||
|
||||
// Long evtime = tasks.get(e.curevtime);
|
||||
// if (evtime == null) {
|
||||
@ -263,7 +259,8 @@ public class StScheduler extends Thread {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
public long runEvents() {
|
||||
|
||||
// System.out.printf("RunEvents in Thread %d\n",Thread.currentThread().getId());
|
||||
@ -279,7 +276,7 @@ public class StScheduler extends Thread {
|
||||
long delay = clock.getDelay(t);
|
||||
System.out.printf("Delay is %d %s\n", delay,StScheduler.formatTimeMillis(clock.currentTimeMillis()));
|
||||
if (delay>0)
|
||||
return delay*2;
|
||||
return delay;
|
||||
|
||||
|
||||
// if (t <= ct) {
|
||||
@ -293,8 +290,10 @@ public class StScheduler extends Thread {
|
||||
if (next_t == -1)
|
||||
continue;
|
||||
|
||||
def.newevtime = next_t + t;
|
||||
this.addTimerTask(def);
|
||||
// def.newevtime = next_t + t;
|
||||
// this.addTimerTask(def);
|
||||
|
||||
|
||||
//System.out.printf("Events in a row: %d\n", s.size());
|
||||
}
|
||||
/*
|
||||
@ -347,11 +346,11 @@ public class StScheduler extends Thread {
|
||||
while (!terminate) {
|
||||
|
||||
while (!new_tasks.isEmpty()) {
|
||||
Event td = new_tasks.poll();
|
||||
NewEvent td = new_tasks.poll();
|
||||
// System.out.printf("There is a set task %d %d\n",td.curevtime,td.newevtime);
|
||||
|
||||
this.cancelMy(td);
|
||||
this.addTimerTask(td);
|
||||
//this.cancelMy(td);
|
||||
this.addTimerTask(td.e,td.t);
|
||||
|
||||
}
|
||||
|
||||
@ -364,7 +363,7 @@ public class StScheduler extends Thread {
|
||||
synchronized (this) {
|
||||
try {
|
||||
// System.out.printf("My WTIME %d\n", wtime);
|
||||
if (wtime != -1 && !pause) {
|
||||
if (wtime != -1 && !clock.isPause()) {
|
||||
wait(wtime);
|
||||
} else {
|
||||
wait();
|
||||
|
Loading…
Reference in New Issue
Block a user