Cleaned up sync issues with Swing Active Rendering

This commit is contained in:
nathan 2017-02-16 00:00:55 +01:00
parent d041655c6d
commit 897c4a83cb
2 changed files with 21 additions and 18 deletions

View File

@ -65,26 +65,28 @@ class ActiveRenderLoop implements Runnable {
actionHandlerLong.handle(updateDeltaNanos); actionHandlerLong.handle(updateDeltaNanos);
} }
for (int i = 0; i < SwingActiveRender.activeRenders.size(); i++) { // this needs to be synchronized because we don't want to our frame removed WHILE we are rendering it.
JFrame jFrame = SwingActiveRender.activeRenders.get(i); synchronized (SwingActiveRender.activeRenders) {
for (int i = 0; i < SwingActiveRender.activeRenders.size(); i++) {
JFrame jFrame = SwingActiveRender.activeRenders.get(i);
final BufferStrategy buffer = jFrame.getBufferStrategy(); final BufferStrategy buffer = jFrame.getBufferStrategy();
// maybe the frame was closed // maybe the frame was closed
if (buffer != null) { if (buffer != null) {
try { try {
graphics = buffer.getDrawGraphics(); graphics = buffer.getDrawGraphics();
jFrame.paint(graphics); jFrame.paint(graphics);
} catch (IllegalStateException ignored) { } catch (Exception e) {
} catch (Exception e) { e.printStackTrace();
e.printStackTrace(); } finally {
} finally { if (graphics != null) {
if (graphics != null) { graphics.dispose();
graphics.dispose();
// blit the back buffer to the screen // blit the back buffer to the screen
if (!buffer.contentsLost()) { if (!buffer.contentsLost()) {
buffer.show(); buffer.show();
}
} }
} }
} }

View File

@ -18,6 +18,7 @@ package dorkbox.util.swing;
import java.awt.Component; import java.awt.Component;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Deque; import java.util.Deque;
import java.util.List; import java.util.List;
@ -41,7 +42,7 @@ public final
class SwingActiveRender { class SwingActiveRender {
private static Thread activeRenderThread = null; private static Thread activeRenderThread = null;
static final List<JFrame> activeRenders = new CopyOnWriteArrayList<JFrame>(); static final List<JFrame> activeRenders = new ArrayList<JFrame>();
static final List<ActionHandlerLong> activeRenderEvents = new CopyOnWriteArrayList<ActionHandlerLong>(); static final List<ActionHandlerLong> activeRenderEvents = new CopyOnWriteArrayList<ActionHandlerLong>();
// volatile, so that access triggers thread synchrony, since 1.6. See the Java Language Spec, Chapter 17 // volatile, so that access triggers thread synchrony, since 1.6. See the Java Language Spec, Chapter 17