Added some stuff.

This commit is contained in:
7u83 2017-01-16 19:12:49 +01:00
parent f4bdace79d
commit 9c60f7c521
2 changed files with 52 additions and 107 deletions

View File

@ -26,14 +26,11 @@
package sesim;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentSkipListSet;
/**
*
@ -42,96 +39,10 @@ import java.util.concurrent.ConcurrentSkipListSet;
public class Scheduler extends Thread {
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;
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() {
stop = true;
synchronized (event_queue) {
@ -142,21 +53,19 @@ public class Scheduler extends Thread {
public interface TimerEvent {
long timerEvent();
}
class ObjectComparator implements Comparator<Object> {
private class ObjectComparator implements Comparator<Object> {
@Override
public int compare(Object o1, Object o2) {
return System.identityHashCode(o1) - System.identityHashCode(o1);
return System.identityHashCode(o1) - System.identityHashCode(o2);
}
}
public long getCurrentTimeMillies() {
return System.currentTimeMillis();
}
public void startEvent(TimerEvent e, long time) {
@ -169,7 +78,7 @@ public class Scheduler extends Thread {
}
s.add(e);
}
synchronized(this){
synchronized (this) {
notify();
}
}
@ -179,13 +88,16 @@ public class Scheduler extends Thread {
}
private void addEvent(TimerEvent e, long time) {
long evtime = time + this.getCurrentTimeMillies();
SortedSet<TimerEvent> s = event_queue.get(evtime);
if (s == null) {
s = new TreeSet<>(new ObjectComparator());
event_queue.put(evtime, s);
}
s.add(e);
}
public long runEvents() {
@ -194,25 +106,35 @@ public class Scheduler extends Thread {
return -1;
}
long t = event_queue.firstKey();
if (t >= this.getCurrentTimeMillies()) {
if (t <= this.getCurrentTimeMillies()) {
SortedSet s = event_queue.get(t);
event_queue.remove(t);
Iterator<TimerEvent> it = s.iterator();
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
public void run() {
while (!stop) {
long wtime = runEvents();
if (wtime == 0) {
continue;
}
synchronized (this) {
try {
@ -225,7 +147,7 @@ public class Scheduler extends Thread {
}
}
}
System.out.print("Running the scheduler\n");
}
}

View File

@ -65,28 +65,51 @@ public class Test {
/**
* @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();
class Ev implements Scheduler.TimerEvent{
@Override
public long timerEvent() {
System.out.print("Timer Event Occured");
return 3000;
System.out.printf("Timer Event Occured %s\n",name);
if ("Ev1".equals(this.name))
return 2000;
else
return 4000;
}
String name;
Ev(String name){
this.name=name;
}
}
Ev e1 = new Ev();
Ev e2 = new Ev();
Ev e1 = new Ev("Ev1");
Ev e2 = new Ev("Eb2");
s.startEvent(e1, 0);
s.startEvent(e2, 0);
while(true){}
try
{
Thread.sleep(90000);
}
catch(Exception e) {
}
s.halt();
while (s.isAlive()){
}
System.out.print("All isstopped\n");
// s.startEvent(e2, 100);