OpenSeSim/src/opensesim/old_sesim/Scheduler.java

389 lines
10 KiB
Java
Raw Normal View History

2017-01-16 07:19:37 +01:00
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* 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.
*/
2018-11-30 12:27:41 +01:00
package opensesim.old_sesim;
2017-01-16 07:19:37 +01:00
2017-02-04 16:29:45 +01:00
//import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
2018-11-30 12:27:41 +01:00
import opensesim.gui.Globals;
2017-02-02 18:39:55 +01:00
import java.text.DateFormat;
import java.text.SimpleDateFormat;
2017-01-16 07:19:37 +01:00
import java.util.Comparator;
2017-02-02 18:39:55 +01:00
import java.util.Date;
2017-02-25 20:06:26 +01:00
import java.util.HashMap;
2017-01-16 07:19:37 +01:00
import java.util.Iterator;
2017-02-26 03:01:24 +01:00
import java.util.LinkedList;
2017-01-16 07:19:37 +01:00
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
2017-02-26 03:01:24 +01:00
import java.util.concurrent.ConcurrentLinkedQueue;
2017-03-06 08:07:29 +01:00
import java.util.concurrent.atomic.AtomicInteger;
2017-01-16 07:19:37 +01:00
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class Scheduler extends Thread {
private double acceleration = 1.0;
2017-02-04 16:29:45 +01:00
public void setAcceleration(double val) {
this.acceleration = val;
synchronized (this) {
2017-02-04 16:29:45 +01:00
this.notify();
}
2017-02-03 01:56:41 +01:00
}
2017-02-04 16:29:45 +01:00
public double getAcceleration() {
2017-02-05 10:24:18 +01:00
return this.acceleration;
2017-02-03 01:56:41 +01:00
}
2017-01-16 07:19:37 +01:00
2017-03-06 08:07:29 +01:00
private final SortedMap<Long, SortedSet<TimerTaskDef>> event_queue = new TreeMap<>();
2017-01-16 07:19:37 +01:00
2017-03-06 08:07:29 +01:00
public interface TimerTaskRunner {
2017-01-16 21:30:10 +01:00
long timerTask();
long getID();
2017-01-16 21:30:10 +01:00
}
2017-02-04 16:29:45 +01:00
private boolean terminate = false;
2017-02-02 18:39:55 +01:00
/**
2017-02-04 16:29:45 +01:00
* Terminate the scheduler
2017-01-16 21:30:10 +01:00
*/
2017-02-04 16:29:45 +01:00
public void terminate() {
terminate = true;
2017-01-16 07:19:37 +01:00
synchronized (event_queue) {
event_queue.notifyAll();
}
2017-02-04 16:29:45 +01:00
}
@Override
public void start() {
if (this.isAlive()) {
return;
}
this.initScheduler();
super.start();
2017-01-16 07:19:37 +01:00
}
2017-01-16 19:12:49 +01:00
private class ObjectComparator implements Comparator<Object> {
2017-01-16 07:19:37 +01:00
@Override
public int compare(Object o1, Object o2) {
2017-03-06 08:07:29 +01:00
return (((TimerTaskRunner) o1).getID() - ((TimerTaskRunner) o2).getID()) < 0 ? -1 : 1;
//return System.identityHashCode(o1) - System.identityHashCode(o2);
2017-01-16 07:19:37 +01:00
}
}
2017-02-02 18:39:55 +01:00
long last_time_millis = System.currentTimeMillis();
double current_time_millis = 0.0;
long last_nanos = System.nanoTime();
double current_nanos = 0;
2017-01-22 07:03:16 +01:00
/**
*
* @return
*/
private long currentTimeMillis1() {
2017-02-04 16:29:45 +01:00
long cur = System.nanoTime();
long diff = cur - last_nanos;
last_nanos = cur;
2017-03-13 19:26:39 +01:00
if (pause) {
return (long) this.current_time_millis;
}
// this.cur_nano += (((double)diff_nano)/1000000.0)*this.acceleration;
// return (long)(cur_nano/1000000.0);
2017-03-13 19:26:39 +01:00
this.current_nanos += (double) diff * (double) this.acceleration;
// this.current_time_millis += ((double) diff) * this.acceleration;
2017-03-13 19:26:39 +01:00
this.current_time_millis = this.current_nanos / 1000000.0;
return (long) this.current_time_millis;
}
2017-03-13 19:26:39 +01:00
2017-02-04 16:29:45 +01:00
public long currentTimeMillis() {
return (long) this.current_time_millis;
2017-02-03 08:47:43 +01:00
}
2017-02-02 18:39:55 +01:00
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;
2017-02-04 16:29:45 +01:00
long seconds = (t / 1000) % 60;
long minutes = (t / 1000 / 60) % 60;
long hours = (t / 1000) / (60 * 60);
2017-01-16 19:12:49 +01:00
return String.format("%d:%02d:%02d", hours, minutes, seconds);
2017-01-16 07:19:37 +01:00
}
2017-03-06 08:07:29 +01:00
AtomicInteger nextTimerTask = new AtomicInteger(0);
2017-02-26 03:01:24 +01:00
2017-03-06 08:07:29 +01:00
public class TimerTaskDef implements Comparable {
TimerTaskRunner taskRunner;
2017-03-06 16:08:20 +01:00
long curevtime;
long newevtime;
2017-03-06 08:07:29 +01:00
int id;
2017-02-26 03:01:24 +01:00
2017-03-06 08:07:29 +01:00
TimerTaskDef(TimerTaskRunner e, long t) {
taskRunner = e;
2017-03-06 16:08:20 +01:00
newevtime = t;
2017-03-06 08:07:29 +01:00
id = nextTimerTask.getAndAdd(1);
}
@Override
public int compareTo(Object o) {
return ((TimerTaskDef) o).id - this.id;
2017-03-06 16:08:20 +01:00
2017-02-26 03:01:24 +01:00
}
}
2017-03-05 16:08:08 +01:00
2017-02-26 03:01:24 +01:00
//LinkedList<TimerTaskDef> set_tasks = new LinkedList<>();
ConcurrentLinkedQueue<TimerTaskDef> set_tasks = new ConcurrentLinkedQueue<>();
2017-03-05 16:08:08 +01:00
2017-01-16 21:30:10 +01:00
/**
2017-02-02 18:39:55 +01:00
*
2017-01-16 21:30:10 +01:00
* @param e
2017-02-02 18:39:55 +01:00
* @param time
2017-03-06 08:07:29 +01:00
* @return The TimerTask created
2017-01-16 21:30:10 +01:00
*/
2017-03-06 08:07:29 +01:00
public TimerTaskDef startTimerTask(TimerTaskRunner e, long time) {
2017-02-26 03:01:24 +01:00
2017-01-22 07:03:16 +01:00
long evtime = time + currentTimeMillis();
2017-03-06 16:08:20 +01:00
TimerTaskDef task = new TimerTaskDef(e, evtime);
2017-03-06 08:07:29 +01:00
set_tasks.add(task);
2017-02-26 03:01:24 +01:00
2017-01-16 19:12:49 +01:00
synchronized (this) {
2017-01-16 07:19:37 +01:00
notify();
}
2017-03-06 08:07:29 +01:00
return task;
2017-01-16 07:19:37 +01:00
}
2017-02-02 18:39:55 +01:00
2017-04-08 07:25:58 +02:00
public void rescheduleTimerTask(TimerTaskDef task, long time) {
2017-03-06 16:08:20 +01:00
long evtime = time + currentTimeMillis();
task.newevtime = evtime;
2017-03-06 16:08:20 +01:00
set_tasks.add(task);
synchronized (this) {
notify();
}
}
2017-02-02 18:39:55 +01:00
private boolean pause = false;
public void pause() {
2017-02-05 10:24:18 +01:00
setPause(!pause);
}
2017-02-25 20:06:26 +01:00
public void setPause(boolean val) {
pause = val;
2017-02-02 18:39:55 +01:00
synchronized (this) {
this.notify();
2017-01-22 21:41:45 +01:00
}
}
2017-02-25 20:06:26 +01:00
public boolean getPause() {
2017-02-05 10:24:18 +01:00
return pause;
}
2017-01-16 07:19:37 +01:00
2017-03-06 08:07:29 +01:00
public long fireEvent(TimerTaskRunner e) {
2017-01-16 21:30:10 +01:00
return e.timerTask();
2017-01-16 07:19:37 +01:00
}
// HashMap<TimerTaskDef, Long> tasks = new HashMap<>();
2017-03-06 16:08:20 +01:00
private boolean addTimerTask(TimerTaskDef e) {
2017-01-16 19:12:49 +01:00
// System.out.printf("Add TimerTask %d %d\n",e.curevtime,e.newevtime);
2017-02-04 16:29:45 +01:00
// long evtime = time + currentTimeMillis();
2017-03-06 16:08:20 +01:00
SortedSet<TimerTaskDef> s = event_queue.get(e.newevtime);
2017-01-16 07:19:37 +01:00
if (s == null) {
2017-03-06 08:07:29 +01:00
s = new TreeSet<>();
2017-03-06 16:08:20 +01:00
event_queue.put(e.newevtime, s);
2017-01-16 07:19:37 +01:00
}
2017-02-26 03:01:24 +01:00
e.curevtime = e.newevtime;
2017-02-25 20:06:26 +01:00
// tasks.put(e, e.evtime);
2017-01-16 21:30:10 +01:00
return s.add(e);
2017-01-16 07:19:37 +01:00
}
2017-03-06 08:07:29 +01:00
private final LinkedList<TimerTaskRunner> cancel_queue = new LinkedList();
2017-02-26 03:01:24 +01:00
2017-03-06 08:07:29 +01:00
public void cancelTimerTask(TimerTaskRunner e) {
2017-02-26 03:01:24 +01:00
cancel_queue.add(e);
}
2017-03-06 08:07:29 +01:00
private void cancelMy(TimerTaskDef e) {
2017-02-26 03:01:24 +01:00
2017-03-06 16:08:20 +01:00
// Long evtime = tasks.get(e.curevtime);
// if (evtime == null) {
// return;
// }
SortedSet<TimerTaskDef> s = event_queue.get(e.curevtime);
2017-02-26 03:01:24 +01:00
if (s == null) {
// System.out.printf("My not found\n");
2017-02-26 03:01:24 +01:00
return;
}
Boolean rc = s.remove(e);
if (s.isEmpty()) {
2017-03-06 16:08:20 +01:00
event_queue.remove(e.curevtime);
2017-02-25 20:06:26 +01:00
}
2017-02-26 03:01:24 +01:00
2017-02-25 20:06:26 +01:00
}
2017-01-16 07:19:37 +01:00
public long runEvents() {
synchronized (event_queue) {
2017-02-26 03:01:24 +01:00
2017-01-16 07:19:37 +01:00
if (event_queue.isEmpty()) {
return -1;
}
long t = event_queue.firstKey();
2017-02-03 08:47:43 +01:00
long ct = currentTimeMillis1();
2017-02-04 16:29:45 +01:00
// ct = t;
2017-03-05 16:08:08 +01:00
if (t > ct) {
2017-03-13 19:26:39 +01:00
//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));
2017-03-13 19:26:39 +01:00
// return (long) diff;
return (long) (((double) t - this.currentTimeMillis()) / this.acceleration);
2017-01-16 07:19:37 +01:00
}
2017-02-26 03:01:24 +01:00
2017-03-13 19:26:39 +01:00
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;
}
2017-03-05 16:08:08 +01:00
// if (t <= ct) {
SortedSet s = event_queue.get(t);
Object rc = event_queue.remove(t);
2017-03-13 19:26:39 +01:00
if (s.size() > 1) {
2017-03-19 18:07:58 +01:00
//System.out.printf("Events in a row: %d\n", s.size());
2017-03-13 19:26:39 +01:00
}
2017-03-06 08:07:29 +01:00
Iterator<TimerTaskDef> it = s.iterator();
2017-03-05 16:08:08 +01:00
while (it.hasNext()) {
2017-03-06 08:07:29 +01:00
TimerTaskDef e = it.next();
// if (s.size() > 1) {
// System.out.printf("Sicku: %d %d\n", e.id, e.curevtime);
// }
2017-03-13 19:26:39 +01:00
2017-03-06 08:07:29 +01:00
long next_t = this.fireEvent(e.taskRunner);
2017-03-06 16:08:20 +01:00
e.newevtime = next_t + t;
this.addTimerTask(e);
2017-03-05 16:08:08 +01:00
}
return 0;
2017-01-16 07:19:37 +01:00
}
2017-01-16 19:12:49 +01:00
2017-01-16 07:19:37 +01:00
}
2017-03-06 08:07:29 +01:00
class EmptyCtr implements TimerTaskRunner {
@Override
public long timerTask() {
2017-03-13 19:26:39 +01:00
// System.out.printf("Current best brice %f\n", Globals.se.getBestPrice());
2017-03-06 08:07:29 +01:00
return 1000;
}
@Override
public long getID() {
return 999999999999999999L;
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
2017-02-04 16:29:45 +01:00
void initScheduler() {
current_time_millis = 0.0;
this.startTimerTask(new EmptyCtr(), 0);
2017-02-04 16:29:45 +01:00
terminate = false;
}
2017-01-16 07:19:37 +01:00
@Override
public void run() {
2017-02-04 16:29:45 +01:00
while (!terminate) {
2017-01-22 21:41:45 +01:00
2017-02-26 03:01:24 +01:00
while (!set_tasks.isEmpty()) {
TimerTaskDef td = set_tasks.poll();
// System.out.printf("There is a set task %d %d\n",td.curevtime,td.newevtime);
2017-03-06 08:07:29 +01:00
this.cancelMy(td);
2017-03-06 16:08:20 +01:00
this.addTimerTask(td);
2017-02-26 03:01:24 +01:00
}
2017-01-16 07:19:37 +01:00
long wtime = runEvents();
2017-02-02 18:39:55 +01:00
2017-01-16 19:12:49 +01:00
if (wtime == 0) {
continue;
}
2017-01-16 07:19:37 +01:00
synchronized (this) {
try {
2017-03-13 07:21:13 +01:00
// System.out.printf("My WTIME %d\n", wtime);
2017-01-23 18:56:09 +01:00
if (wtime != -1 && !pause) {
2017-01-16 07:19:37 +01:00
wait(wtime);
} else {
wait();
}
} catch (Exception e) {
2017-01-16 21:30:10 +01:00
2017-01-16 07:19:37 +01:00
}
}
}
2017-01-16 19:12:49 +01:00
2017-02-04 16:29:45 +01:00
this.event_queue.clear();
2017-01-16 07:19:37 +01:00
}
}