From 9542131e4a52613db32e20a05fd4a3d1c01642ba Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Thu, 6 Nov 2014 17:34:20 +0100 Subject: [PATCH] Avoid creating a new anonymous Runnable each frame. Since the Runnable that is passed to the EventQueue does never change, it now is created in the constructor. This should avoid a lot of overhead. --- .../spacescooter/thread/PaintThread.java | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/de/teamteamteam/spacescooter/thread/PaintThread.java b/src/de/teamteamteam/spacescooter/thread/PaintThread.java index 0adfd20..b8bc5ca 100644 --- a/src/de/teamteamteam/spacescooter/thread/PaintThread.java +++ b/src/de/teamteamteam/spacescooter/thread/PaintThread.java @@ -5,23 +5,33 @@ import java.awt.EventQueue; import de.teamteamteam.spacescooter.gui.GameFrame; /** - * This thread triggers about 60 redraws per second. + * This thread triggers the redrawing on the GameFrame. */ public class PaintThread extends TimedThread { - private GameFrame gf; - + /** + * Runnable that is passed to the EventQueue for later invocation. + */ + private final Runnable paintRunnable; + + /** + * Constructor. Sets the name of the Thread and creates the paintRunnable. + */ public PaintThread(GameFrame gf) { - this.gf = gf; + final GameFrame gameFrame = gf; this.setName("PaintThread"); + this.paintRunnable = new Runnable() { + public void run() { + gameFrame.draw(); + } + }; } + /** + * The work method invoked by the TimingThread. + */ public void work() { //Trigger redrawing the things. Important: AWT-Context needed here! - EventQueue.invokeLater(new Runnable() { - public void run() { - gf.draw(); - } - }); + EventQueue.invokeLater(this.paintRunnable); } }