Refactorings zur Nebenläufigkeit

This commit is contained in:
ngb
2022-07-25 17:45:39 +02:00
parent 7772793e8d
commit 617b915874
2 changed files with 26 additions and 14 deletions

View File

@@ -42,7 +42,8 @@ public final class Options {
STOPPED, STOPPED,
TERMINATED, TERMINATED,
IDLE, IDLE,
DELAYED DELAYED,
DISPATCHING
} }
/** /**

View File

@@ -410,17 +410,20 @@ public class Zeichenmaschine extends Constants {
public void windowClosing( WindowEvent e ) { public void windowClosing( WindowEvent e ) {
if( running ) { if( running ) {
running = false; running = false;
teardown(); mainThread.interrupt();
cleanup(); //teardown();
//cleanup();
} }
// Give the app a minimum amount of time to shut down // Give the app a minimum amount of time to shut down
// then kill it. // then kill it.
try { while( state != Options.AppState.TERMINATED ) {
Thread.sleep(5); Thread.yield();
} catch( InterruptedException ex ) { if( Thread.interrupted() ) {
} finally { break;
quit(true); }
} }
// Quit
quit(true);
} }
}); });
@@ -996,7 +999,7 @@ public class Zeichenmaschine extends Constants {
updateState = Options.AppState.DELAYED; updateState = Options.AppState.DELAYED;
Thread.sleep(ms - sub, (int) (timer % 1000000L)); Thread.sleep(ms - sub, (int) (timer % 1000000L));
} catch( InterruptedException ex ) { } catch( InterruptedException ignored ) {
// Nothing // Nothing
} finally { } finally {
updateState = oldState; updateState = oldState;
@@ -1198,10 +1201,12 @@ public class Zeichenmaschine extends Constants {
} }
/* /*
* Mouse handling * Input handling
*/ */
private void enqueueEvent( InputEvent evt ) { private void enqueueEvent( InputEvent evt ) {
eventQueue.add(evt); if( updateState != Options.AppState.DELAYED ) {
eventQueue.add(evt);
}
if( isPaused() || isStopped() ) { if( isPaused() || isStopped() ) {
dispatchEvents(); dispatchEvents();
@@ -1209,7 +1214,7 @@ public class Zeichenmaschine extends Constants {
} }
private void dispatchEvents() { private void dispatchEvents() {
synchronized( eventQueue ) { //synchronized( eventQueue ) {
while( !eventQueue.isEmpty() ) { while( !eventQueue.isEmpty() ) {
InputEvent evt = eventQueue.poll(); InputEvent evt = eventQueue.poll();
@@ -1230,7 +1235,7 @@ public class Zeichenmaschine extends Constants {
break; break;
} }
} }
} //}
} }
private void handleKeyEvent( KeyEvent evt ) { private void handleKeyEvent( KeyEvent evt ) {
@@ -1469,10 +1474,11 @@ public class Zeichenmaschine extends Constants {
// Call to draw() // Call to draw()
updateState = Options.AppState.DRAWING; updateState = Options.AppState.DRAWING;
Zeichenmaschine.this.draw(); Zeichenmaschine.this.draw();
updateState = Options.AppState.IDLE; updateState = Options.AppState.DISPATCHING;
// Send latest input events after finishing draw // Send latest input events after finishing draw
// since these may also block // since these may also block
dispatchEvents(); dispatchEvents();
updateState = Options.AppState.IDLE;
} }
}); });
} }
@@ -1481,6 +1487,11 @@ public class Zeichenmaschine extends Constants {
while( updateThreadExecutor.isRunning() while( updateThreadExecutor.isRunning()
&& !updateThreadExecutor.isWaiting() ) { && !updateThreadExecutor.isWaiting() ) {
Thread.yield(); Thread.yield();
if( Thread.interrupted() ) {
running = false;
break;
}
} }
// Display the current buffer content // Display the current buffer content