diff --git a/src/example/philosophes/MainPhilosophe.java b/src/example/philosophes/MainPhilosophe.java index 64e03674e413d37a975e085dca8d0d0e350a4cf4..aaa05b05db64b3209a8eddedff27a6b3cb3793cd 100644 --- a/src/example/philosophes/MainPhilosophe.java +++ b/src/example/philosophes/MainPhilosophe.java @@ -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"); diff --git a/src/example/philosophes/Philosopher.java b/src/example/philosophes/Philosopher.java index 18831e4b77cefc0a83d57652173b5bbc8c6dfd3a..5ae3fb23f93e7c93504203c195e44e8a430f312a 100644 --- a/src/example/philosophes/Philosopher.java +++ b/src/example/philosophes/Philosopher.java @@ -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)); } diff --git a/src/mas/core/Schedulable.java b/src/mas/core/Schedulable.java index a868b93c20b35a570b98dbf399f6456272150c7f..f09083297627b1a040ec1b018a70d745a29492ee 100644 --- a/src/mas/core/Schedulable.java +++ b/src/mas/core/Schedulable.java @@ -50,4 +50,14 @@ public interface Schedulable { * @return */ boolean stopCondition(); + + /** + * TODO + */ + boolean isFinished(); + + /** + * TODO + */ + void waitUntilFinish(); } diff --git a/src/mas/implementation/schedulers/AsyncCycling.java b/src/mas/implementation/schedulers/AsyncCycling.java index 2b5073cb9daac1214fdde6723a42acef5b15fba9..1e09c47fea3b70e588dfb5e0884238ae6d56397e 100644 --- a/src/mas/implementation/schedulers/AsyncCycling.java +++ b/src/mas/implementation/schedulers/AsyncCycling.java @@ -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; diff --git a/src/mas/implementation/schedulers/FairCycling.java b/src/mas/implementation/schedulers/FairCycling.java index 2383a5646e92101300cb1f89827e6d5584058531..429e191c34e2638e96c311915c61e4fe9621db8e 100644 --- a/src/mas/implementation/schedulers/FairCycling.java +++ b/src/mas/implementation/schedulers/FairCycling.java @@ -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() { } diff --git a/src/mas/implementation/schedulers/FairPosCycling.java b/src/mas/implementation/schedulers/FairPosCycling.java index 59bec2bb82c4938b2da6b45e64ff255c91c6ea0c..7881682bb5d64394d5ec25ba0f2b05f4444f8e2c 100644 --- a/src/mas/implementation/schedulers/FairPosCycling.java +++ b/src/mas/implementation/schedulers/FairPosCycling.java @@ -46,4 +46,14 @@ public class FairPosCycling implements Schedulable { public boolean stopCondition() { return false; } + + @Override + public boolean isFinished() { + return false; + } + + @Override + public void waitUntilFinish() { + + } } diff --git a/src/mas/implementation/schedulers/variations/TwoDCycling.java b/src/mas/implementation/schedulers/variations/TwoDCycling.java index 78cf03a45ed3d0f13843ede350e6336f16dbf466..0a886d2736dfb8b7a5ce036d532a2baa2b24f633 100644 --- a/src/mas/implementation/schedulers/variations/TwoDCycling.java +++ b/src/mas/implementation/schedulers/variations/TwoDCycling.java @@ -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 */