Skip to content
Snippets Groups Projects
Commit 78641cf7 authored by David Antunes's avatar David Antunes
Browse files

Add isFinished() and waitUntilFinish() in Schedulable interface

parent b46057c1
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,6 @@ package example.philosophes;
import mas.core.Schedulable;
import mas.implementation.schedulers.FairCycling;
import mas.implementation.schedulers.variations.ThreeStepCycling;
public class MainPhilosophe {
......@@ -28,7 +27,7 @@ public class MainPhilosophe {
philosophers[i].setRightPhilosopher(philosophers[(i+1) % nAgents]);
}
Schedulable scheduler = new ThreeStepCycling(philosophers);
Schedulable scheduler = new FairCycling(philosophers);
scheduler.setSleep(100);
scheduler.start();
......@@ -47,6 +46,7 @@ public class MainPhilosophe {
scheduler.resume();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
......@@ -55,6 +55,8 @@ public class MainPhilosophe {
scheduler.stop();
scheduler.waitUntilFinish();
final long endTime = System.nanoTime();
System.out.println("Total execution time: " + (endTime / 1000000 - startTime / 1000000) + " microseconds");
......
......@@ -65,13 +65,13 @@ public class Philosopher extends Agent {
@Override
public void perceive() {
System.out.println("Philosopher num " + id + " perceive");
//System.out.println("Philosopher num " + id + " perceive");
criticallity = computeCriticallity();
}
@Override
public void decide() {
System.out.println("Philosopher num " + id + " decide");
//System.out.println("Philosopher num " + id + " decide");
State nextState = state;
switch (state) {
......@@ -118,7 +118,7 @@ public class Philosopher extends Agent {
@Override
public void act() {
System.out.println("Philosopher num " + id + " act");
//System.out.println("Philosopher num " + id + " act");
scheduler.addCyclable(new Waste(id));
}
......
......@@ -50,4 +50,14 @@ public interface Schedulable {
* @return
*/
boolean stopCondition();
/**
* TODO
*/
boolean isFinished();
/**
* TODO
*/
void waitUntilFinish();
}
......@@ -74,6 +74,20 @@ public class AsyncCycling implements Schedulable {
return false;
}
@Override
public boolean isFinished() {
return executor.isTerminated();
}
@Override
public void waitUntilFinish() {
try {
executor.awaitTermination(Long.MAX_VALUE,TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public int getSleep() {
return sleep;
......
......@@ -63,6 +63,20 @@ public class FairCycling implements Schedulable {
return false;
}
@Override
public boolean isFinished() {
return executor.isTerminated();
}
@Override
public void waitUntilFinish() {
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
protected void onCycleEnds() {
}
......
......@@ -46,4 +46,14 @@ public class FairPosCycling implements Schedulable {
public boolean stopCondition() {
return false;
}
@Override
public boolean isFinished() {
return false;
}
@Override
public void waitUntilFinish() {
}
}
......@@ -27,7 +27,7 @@ public class TwoDCycling extends FairCycling {
*/
private List<Consumer<TwoDCycling>> onChange = new ArrayList<>();
public enum State {
protected enum State {
/**
* The scheduler is waiting to start
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment