From ced0aa6842699f0466aed0a0b5144d1caa6165c2 Mon Sep 17 00:00:00 2001 From: "J. Neugebauer" Date: Fri, 8 Jul 2022 07:44:49 +0200 Subject: [PATCH] =?UTF-8?q?Synchronisation=20=C3=BCber=20einen=20globalen?= =?UTF-8?q?=20Monitor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schule/ngb/zm/Zeichenmaschine.java | 35 +++++++++++++++++-- .../ngb/zm/tasks/FrameSynchronizedTask.java | 9 +++-- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/schule/ngb/zm/Zeichenmaschine.java b/src/schule/ngb/zm/Zeichenmaschine.java index 151de10..93ed677 100644 --- a/src/schule/ngb/zm/Zeichenmaschine.java +++ b/src/schule/ngb/zm/Zeichenmaschine.java @@ -1276,10 +1276,37 @@ public class Zeichenmaschine extends Constants { // Zeichenthread //// + /** + * Globaler Monitor, der einmal pro Frame vom Zeichenthread freigegeben + * wird. Andere Threads können {@link Object#wait()} auf dem Monitor + * aufrufen, um sich mit dem Zeichenthread zu synchronisieren. Der + * {@code wait()} Aufruf sollte sich zur Sicherheit in einer Schleife + * befinden, die prüft, ob sich der Aktuelle {@link #tick} erhöht hat. + *

+	 * int lastTick = Constants.tick;
+	 *
+	 * // Do some work
+	 *
+	 * while( lastTick >= Constants.tick ) {
+	 *     synchronized( Zeichenmaschine.globalSyncLock ) {
+	 *         try {
+	 *             Zeichenmaschine.globalSyncLock.wait();
+	 *         } catch( InterruptedException ex ) {}
+	 *     }
+	 * }
+	 * // Next frame has started
+	 * 
+ *

+ * Die {@link schule.ngb.zm.tasks.FrameSynchronizedTask} implementiert eine + * {@link schule.ngb.zm.tasks.Task}, die sich automatisch auf diese Wiese + * mit dem Zeichenthread synchronisiert. + */ + public static final Object globalSyncLock = new Object[0]; + class Zeichenthread extends Thread { public Zeichenthread() { - super(Zeichenthread.class.getSimpleName()); + super(APP_NAME); //super(APP_NAME + " " + APP_VERSION); } @@ -1328,6 +1355,9 @@ public class Zeichenmaschine extends Constants { } runTasks(); + synchronized( globalSyncLock ) { + globalSyncLock.notifyAll(); + } // delta time in ns long afterTime = System.nanoTime(); @@ -1413,7 +1443,8 @@ public class Zeichenmaschine extends Constants { } - class InputListener implements MouseInputListener, MouseMotionListener, MouseWheelListener, KeyListener{ + class InputListener implements MouseInputListener, MouseMotionListener, MouseWheelListener, KeyListener { + @Override public void mouseClicked( MouseEvent e ) { enqueueEvent(e); diff --git a/src/schule/ngb/zm/tasks/FrameSynchronizedTask.java b/src/schule/ngb/zm/tasks/FrameSynchronizedTask.java index 73bda60..416b9db 100644 --- a/src/schule/ngb/zm/tasks/FrameSynchronizedTask.java +++ b/src/schule/ngb/zm/tasks/FrameSynchronizedTask.java @@ -10,7 +10,7 @@ public abstract class FrameSynchronizedTask extends Task { private static final Thread getMainThread() { if( mainThread == null ) { mainThread = Thread.currentThread(); - if( !mainThread.getName().equals("Zeichenthread") ) { + if( !mainThread.getName().equals(Constants.APP_NAME) ) { // Need to search for main Zeichenthread ... } } @@ -21,7 +21,7 @@ public abstract class FrameSynchronizedTask extends Task { public void run() { running = true; int lastTick = 0; - Thread lock = getMainThread(); + Object lock = Zeichenmaschine.globalSyncLock; while( running ) { lastTick = Constants.tick; @@ -29,12 +29,11 @@ public abstract class FrameSynchronizedTask extends Task { synchronized( lock ) { while( lastTick >= Constants.tick ) { - /*try { + try { lock.wait(); } catch( InterruptedException e ) { // We got interrupted ... - }*/ - Thread.yield(); + } } } }