X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=LogicUI%2Fsrc%2Fera%2Fmi%2Fgui%2FLogicExecuter.java;h=65abd4c9c4248f447712561d43ca6cb456b64e13;hb=09cec3a0d9a1e33dafa099d1e0b36195c054c15b;hp=d5ffd862335904b11f89c345e8751857acc14d2b;hpb=29cfa7eb267542506beb197e54af8aa3f642c3fb;p=Mograsim.git diff --git a/LogicUI/src/era/mi/gui/LogicExecuter.java b/LogicUI/src/era/mi/gui/LogicExecuter.java index d5ffd862..65abd4c9 100644 --- a/LogicUI/src/era/mi/gui/LogicExecuter.java +++ b/LogicUI/src/era/mi/gui/LogicExecuter.java @@ -5,6 +5,7 @@ import java.util.concurrent.atomic.AtomicLong; import era.mi.logic.timeline.Timeline; +//TODO maybe move to logic core? public class LogicExecuter { // TODO replace with LogicModel when it exists @@ -26,6 +27,10 @@ public class LogicExecuter simulationThread = new Thread(() -> { isRunningLive.set(true); + synchronized (isRunningLive) + { + isRunningLive.notify(); + } while (shouldBeRunningLive.get()) { // always execute to keep timeline from "hanging behind" for too long @@ -42,11 +47,15 @@ public class LogicExecuter if (sleepTime > 0) Thread.sleep(sleepTime); } - catch (InterruptedException e) + catch (@SuppressWarnings("unused") InterruptedException e) {// do nothing; it is normal execution flow to be interrupted } } isRunningLive.set(false); + synchronized (isRunningLive) + { + isRunningLive.notify(); + } }); timeline.addEventAddedListener(event -> { @@ -67,8 +76,7 @@ public class LogicExecuter return; shouldBeRunningLive.set(true); simulationThread.start(); - while (!isRunningLive.get()) - ; + waitForIsRunning(true); } public synchronized void stopLiveExecution() @@ -77,7 +85,21 @@ public class LogicExecuter return; shouldBeRunningLive.set(false); simulationThread.interrupt(); - while (isRunningLive.get()) - ; + waitForIsRunning(false); + } + + private void waitForIsRunning(boolean expectedState) + { + while (isRunningLive.get() ^ expectedState) + try + { + synchronized (isRunningLive) + { + isRunningLive.wait(); + } + } + catch (@SuppressWarnings("unused") InterruptedException e) + {// no need to do anything + } } } \ No newline at end of file