From c8fb6026b41f4292197ed7525d6e54a78a88add0 Mon Sep 17 00:00:00 2001 From: hneemann Date: Fri, 1 Apr 2016 13:15:48 +0200 Subject: [PATCH] switched to Scheduled Executor because old timer is not working on Windows --- src/main/dig/processor/Processor.dig | 116 +++++++++--------- .../digital/draw/model/RealTimeClock.java | 21 ++-- .../java/de/neemann/digital/gui/Main.java | 5 +- 3 files changed, 73 insertions(+), 69 deletions(-) diff --git a/src/main/dig/processor/Processor.dig b/src/main/dig/processor/Processor.dig index 9eaa31b05..821b63e13 100644 --- a/src/main/dig/processor/Processor.dig +++ b/src/main/dig/processor/Processor.dig @@ -107,9 +107,7 @@ rotation - - 1 - + Bits @@ -228,9 +226,7 @@ rotation - - 1 - + Label @@ -238,7 +234,7 @@ Frequency - 100 + 1000 @@ -277,166 +273,166 @@ GPO.dig - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 LED - + 0 - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + @@ -459,8 +455,8 @@ - - + + @@ -532,7 +528,7 @@ - + @@ -592,7 +588,7 @@ - + @@ -664,7 +660,7 @@ - + @@ -716,7 +712,7 @@ - + diff --git a/src/main/java/de/neemann/digital/draw/model/RealTimeClock.java b/src/main/java/de/neemann/digital/draw/model/RealTimeClock.java index 7fe575ac7..8e0368405 100644 --- a/src/main/java/de/neemann/digital/draw/model/RealTimeClock.java +++ b/src/main/java/de/neemann/digital/draw/model/RealTimeClock.java @@ -6,19 +6,23 @@ import de.neemann.digital.gui.GuiModelObserver; import de.neemann.gui.ErrorMessage; import javax.swing.*; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; /** * @author hneemann */ public class RealTimeClock implements ModelStateObserver { - private final Model model; + private final ScheduledThreadPoolExecutor executor; private final int frequency; private final ObservableValue output; - private Timer timer; + private ScheduledFuture timer; - public RealTimeClock(Model model, Clock clock) { + public RealTimeClock(Model model, Clock clock, ScheduledThreadPoolExecutor executor) { this.model = model; + this.executor = executor; int f = clock.getFrequency(); if (f < 1) f = 1; this.frequency = f; @@ -34,20 +38,21 @@ public class RealTimeClock implements ModelStateObserver { int delay = 1000 / frequency; if (delay < 1) delay = 1; - timer = new Timer(delay, e -> { + + timer = executor.scheduleAtFixedRate(() -> SwingUtilities.invokeLater(() -> { output.setValue(1 - output.getValue()); try { model.doStep(); } catch (NodeException e1) { SwingUtilities.invokeLater(new ErrorMessage("ClockError").addCause(e1)); - timer.stop(); + timer.cancel(false); } - }); - timer.start(); + }), delay, delay, TimeUnit.MILLISECONDS); + break; case STOPPED: if (timer != null) - timer.stop(); + timer.cancel(false); break; } } diff --git a/src/main/java/de/neemann/digital/gui/Main.java b/src/main/java/de/neemann/digital/gui/Main.java index 36e890b7a..d01b64f0a 100644 --- a/src/main/java/de/neemann/digital/gui/Main.java +++ b/src/main/java/de/neemann/digital/gui/Main.java @@ -24,6 +24,7 @@ import java.awt.event.WindowEvent; import java.io.Closeable; import java.io.File; import java.io.IOException; +import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.prefs.Preferences; /** @@ -58,6 +59,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave { private Model model; private ModelDescription modelDescription; private boolean modelHasRunningClocks; + private ScheduledThreadPoolExecutor timerExecuter = new ScheduledThreadPoolExecutor(1); private Main() { this(null, null, null); @@ -95,6 +97,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave { @Override public void windowClosed(WindowEvent e) { clearModelDescription(); // stop model timer if running + timerExecuter.shutdown(); } }); @@ -372,7 +375,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave { if (runClock) for (Clock c : model.getClocks()) - model.addObserver(new RealTimeClock(model, c)); + model.addObserver(new RealTimeClock(model, c, timerExecuter)); model.init();