-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle UI update dispatch via a separate thread
This avoids queuing too many updates for the event dispatch thread via invokeLater(), but also avoids slowing down processing by directly using invokeAndWait() in the worker. The benefits w.r.t. invokeAndWait() seem small and inconsistent, to be honest, but in one case the improvement was of ~10 cycles/sec (on a speed of 50 cycles/sec). The code is not too complex so I am going for this approach.
- Loading branch information
Showing
2 changed files
with
97 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* GUIUpdateThread.java | ||
* | ||
* Thread that updates the UI according to a specified frequency. Used to | ||
* rate-limit UI update requests from the CPUSwingWorker. | ||
* | ||
* (c) 2017 Andrea Spadaccini | ||
* | ||
* This file is part of the EduMIPS64 project, and is released under the GNU | ||
* General Public License. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
*/ | ||
package org.edumips64.ui.swing; | ||
|
||
import javax.swing.*; | ||
import java.lang.reflect.InvocationTargetException; | ||
|
||
public class GUIUpdateThread extends Thread { | ||
private boolean shouldUpdate = false; | ||
private boolean shouldTerminate = false; | ||
private GUIFrontend frontend; | ||
private long periodMs; | ||
|
||
GUIUpdateThread(GUIFrontend frontend) { | ||
this.frontend = frontend; | ||
// Rate-limiting to 100 updates / second. | ||
periodMs = 10; | ||
} | ||
|
||
// Signals to this thread that an update is necessary. The logic that handles this boolean | ||
// is not synchronized because it is not important to get it perfectly correct. | ||
void triggerUpdate() { | ||
shouldUpdate = true; | ||
} | ||
|
||
// Signals to this thread that it should stop executing as quickly as possible. | ||
void terminate() { | ||
shouldTerminate = true; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
while(true) { | ||
if (shouldTerminate) { | ||
break; | ||
} | ||
long start = System.currentTimeMillis(); | ||
if (shouldUpdate) { | ||
try { | ||
SwingUtilities.invokeAndWait(() -> { | ||
frontend.updateComponents(); | ||
frontend.represent(); | ||
}); | ||
shouldUpdate = false; | ||
} catch (InterruptedException | InvocationTargetException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
long end = System.currentTimeMillis(); | ||
try { | ||
Thread.sleep(end - start); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |