Added some stuff.
This commit is contained in:
parent
f4bdace79d
commit
9c60f7c521
@ -26,14 +26,11 @@
|
|||||||
package sesim;
|
package sesim;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.SortedMap;
|
import java.util.SortedMap;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
import java.util.concurrent.ConcurrentSkipListSet;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -42,96 +39,10 @@ import java.util.concurrent.ConcurrentSkipListSet;
|
|||||||
public class Scheduler extends Thread {
|
public class Scheduler extends Thread {
|
||||||
|
|
||||||
long multiply = 1;
|
long multiply = 1;
|
||||||
final SortedMap<Long, SortedSet<TimerEvent>> event_queue = new TreeMap<>();
|
|
||||||
|
|
||||||
|
private final SortedMap<Long, SortedSet<TimerEvent>> event_queue = new TreeMap<>();
|
||||||
private boolean stop = false;
|
private boolean stop = false;
|
||||||
|
|
||||||
public class Runner extends Thread {
|
|
||||||
|
|
||||||
Runner() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
while (!stop) {
|
|
||||||
synchronized (event_queue) {
|
|
||||||
try {
|
|
||||||
event_queue.wait(5000);
|
|
||||||
} catch (Exception e) {
|
|
||||||
|
|
||||||
}
|
|
||||||
System.out.printf("500 mllies waited\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private class Event implements Comparable {
|
|
||||||
|
|
||||||
long time;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compareTo(Object o) {
|
|
||||||
if (((Event) o).time < time) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (((Event) o).time > time) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Runner queues[];
|
|
||||||
Runner runners[];
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param num_runners
|
|
||||||
*/
|
|
||||||
public Scheduler(int num_runners) {
|
|
||||||
// event_queue = new ConcurrentSkipListSet(new TreeSet<>());
|
|
||||||
runners = new Runner[num_runners];
|
|
||||||
|
|
||||||
for (int i = 0; i < num_runners; i++) {
|
|
||||||
Runner r = new Runner();
|
|
||||||
System.out.print("Making runner: " + r + "\n");
|
|
||||||
|
|
||||||
runners[i] = new Runner();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void start() {
|
|
||||||
|
|
||||||
/* for (Runner runner : runners) {
|
|
||||||
runner.start();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
super.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
public boolean isAlive() {
|
|
||||||
|
|
||||||
for (Runner runner : runners) {
|
|
||||||
if (runner.isAlive()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
public void halt() {
|
public void halt() {
|
||||||
stop = true;
|
stop = true;
|
||||||
synchronized (event_queue) {
|
synchronized (event_queue) {
|
||||||
@ -142,16 +53,14 @@ public class Scheduler extends Thread {
|
|||||||
public interface TimerEvent {
|
public interface TimerEvent {
|
||||||
|
|
||||||
long timerEvent();
|
long timerEvent();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ObjectComparator implements Comparator<Object> {
|
private class ObjectComparator implements Comparator<Object> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compare(Object o1, Object o2) {
|
public int compare(Object o1, Object o2) {
|
||||||
return System.identityHashCode(o1) - System.identityHashCode(o1);
|
return System.identityHashCode(o1) - System.identityHashCode(o2);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getCurrentTimeMillies() {
|
public long getCurrentTimeMillies() {
|
||||||
@ -179,13 +88,16 @@ public class Scheduler extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addEvent(TimerEvent e, long time) {
|
private void addEvent(TimerEvent e, long time) {
|
||||||
|
|
||||||
long evtime = time + this.getCurrentTimeMillies();
|
long evtime = time + this.getCurrentTimeMillies();
|
||||||
|
|
||||||
SortedSet<TimerEvent> s = event_queue.get(evtime);
|
SortedSet<TimerEvent> s = event_queue.get(evtime);
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
s = new TreeSet<>(new ObjectComparator());
|
s = new TreeSet<>(new ObjectComparator());
|
||||||
event_queue.put(evtime, s);
|
event_queue.put(evtime, s);
|
||||||
}
|
}
|
||||||
s.add(e);
|
s.add(e);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public long runEvents() {
|
public long runEvents() {
|
||||||
@ -194,25 +106,35 @@ public class Scheduler extends Thread {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
long t = event_queue.firstKey();
|
long t = event_queue.firstKey();
|
||||||
if (t >= this.getCurrentTimeMillies()) {
|
if (t <= this.getCurrentTimeMillies()) {
|
||||||
SortedSet s = event_queue.get(t);
|
SortedSet s = event_queue.get(t);
|
||||||
|
|
||||||
event_queue.remove(t);
|
event_queue.remove(t);
|
||||||
Iterator<TimerEvent> it = s.iterator();
|
Iterator<TimerEvent> it = s.iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
long next_t = this.fireEvent(it.next());
|
|
||||||
|
|
||||||
|
TimerEvent e = it.next();
|
||||||
|
long next_t = this.fireEvent(e);
|
||||||
|
this.addEvent(e, next_t);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return t - this.getCurrentTimeMillies();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (!stop) {
|
while (!stop) {
|
||||||
long wtime = runEvents();
|
long wtime = runEvents();
|
||||||
|
if (wtime == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
try {
|
try {
|
||||||
@ -225,7 +147,7 @@ public class Scheduler extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.print("Running the scheduler\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -65,28 +65,51 @@ public class Test {
|
|||||||
/**
|
/**
|
||||||
* @param args the command line arguments
|
* @param args the command line arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
|
||||||
Scheduler s = new Scheduler(10);
|
Scheduler s = new Scheduler();
|
||||||
s.start();
|
s.start();
|
||||||
|
|
||||||
class Ev implements Scheduler.TimerEvent{
|
class Ev implements Scheduler.TimerEvent{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long timerEvent() {
|
public long timerEvent() {
|
||||||
System.out.print("Timer Event Occured");
|
System.out.printf("Timer Event Occured %s\n",name);
|
||||||
return 3000;
|
if ("Ev1".equals(this.name))
|
||||||
|
return 2000;
|
||||||
|
else
|
||||||
|
return 4000;
|
||||||
|
}
|
||||||
|
|
||||||
|
String name;
|
||||||
|
Ev(String name){
|
||||||
|
this.name=name;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ev e1 = new Ev();
|
Ev e1 = new Ev("Ev1");
|
||||||
Ev e2 = new Ev();
|
Ev e2 = new Ev("Eb2");
|
||||||
|
|
||||||
|
|
||||||
s.startEvent(e1, 0);
|
s.startEvent(e1, 0);
|
||||||
|
s.startEvent(e2, 0);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Thread.sleep(90000);
|
||||||
|
}
|
||||||
|
catch(Exception e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
s.halt();
|
||||||
|
while (s.isAlive()){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.print("All isstopped\n");
|
||||||
|
|
||||||
while(true){}
|
|
||||||
// s.startEvent(e2, 100);
|
// s.startEvent(e2, 100);
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user