mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-04-14 06:33:34 +02:00
Synchronisation über einen globalen Monitor
This commit is contained in:
@@ -1276,10 +1276,37 @@ public class Zeichenmaschine extends Constants {
|
|||||||
// Zeichenthread
|
// 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.
|
||||||
|
* <pre><code>
|
||||||
|
* 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
|
||||||
|
* </code></pre>
|
||||||
|
* <p>
|
||||||
|
* 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 {
|
class Zeichenthread extends Thread {
|
||||||
|
|
||||||
public Zeichenthread() {
|
public Zeichenthread() {
|
||||||
super(Zeichenthread.class.getSimpleName());
|
super(APP_NAME);
|
||||||
//super(APP_NAME + " " + APP_VERSION);
|
//super(APP_NAME + " " + APP_VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1328,6 +1355,9 @@ public class Zeichenmaschine extends Constants {
|
|||||||
}
|
}
|
||||||
|
|
||||||
runTasks();
|
runTasks();
|
||||||
|
synchronized( globalSyncLock ) {
|
||||||
|
globalSyncLock.notifyAll();
|
||||||
|
}
|
||||||
|
|
||||||
// delta time in ns
|
// delta time in ns
|
||||||
long afterTime = System.nanoTime();
|
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
|
@Override
|
||||||
public void mouseClicked( MouseEvent e ) {
|
public void mouseClicked( MouseEvent e ) {
|
||||||
enqueueEvent(e);
|
enqueueEvent(e);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ public abstract class FrameSynchronizedTask extends Task {
|
|||||||
private static final Thread getMainThread() {
|
private static final Thread getMainThread() {
|
||||||
if( mainThread == null ) {
|
if( mainThread == null ) {
|
||||||
mainThread = Thread.currentThread();
|
mainThread = Thread.currentThread();
|
||||||
if( !mainThread.getName().equals("Zeichenthread") ) {
|
if( !mainThread.getName().equals(Constants.APP_NAME) ) {
|
||||||
// Need to search for main Zeichenthread ...
|
// Need to search for main Zeichenthread ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,7 @@ public abstract class FrameSynchronizedTask extends Task {
|
|||||||
public void run() {
|
public void run() {
|
||||||
running = true;
|
running = true;
|
||||||
int lastTick = 0;
|
int lastTick = 0;
|
||||||
Thread lock = getMainThread();
|
Object lock = Zeichenmaschine.globalSyncLock;
|
||||||
|
|
||||||
while( running ) {
|
while( running ) {
|
||||||
lastTick = Constants.tick;
|
lastTick = Constants.tick;
|
||||||
@@ -29,12 +29,11 @@ public abstract class FrameSynchronizedTask extends Task {
|
|||||||
|
|
||||||
synchronized( lock ) {
|
synchronized( lock ) {
|
||||||
while( lastTick >= Constants.tick ) {
|
while( lastTick >= Constants.tick ) {
|
||||||
/*try {
|
try {
|
||||||
lock.wait();
|
lock.wait();
|
||||||
} catch( InterruptedException e ) {
|
} catch( InterruptedException e ) {
|
||||||
// We got interrupted ...
|
// We got interrupted ...
|
||||||
}*/
|
}
|
||||||
Thread.yield();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user