Skip to content

Commit

Permalink
add shutdown hooks to GUIView and MainCommunicator
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-szarkowicz committed May 18, 2024
1 parent 4400c86 commit bd8de47
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/core/src/temalab/communicator/MainCommunicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class MainCommunicator{
private boolean pause;
private List<Communicator> communictors;
private boolean runCommThread;
private Thread shutdownHook;

public MainCommunicator(MainModel mm) {
communictors = new ArrayList<>();
Expand Down Expand Up @@ -50,9 +51,16 @@ public void run() {
}
};
commThread.start();
shutdownHook = new Thread(this::stop);
Runtime.getRuntime().addShutdownHook(shutdownHook);
}

public void stop() {
synchronized(shutdownHook) {
if (!Runtime.getRuntime().removeShutdownHook(shutdownHook)) {
return;
}
}
runCommThread = false;
commThread.interrupt();
try {
Expand Down
8 changes: 8 additions & 0 deletions test/core/src/temalab/gui/view/GUIView.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class GUIView extends ApplicationAdapter implements MainModelListener{
private Map<Team, TeamView> teamViews;
private MainModel mm;
private MainCommunicator mc;
private Thread shutdownHook;

public void init(MainModel mm, MainCommunicator mc, float sizingFactor, boolean steppable) {
universalDistanceConstant = sizingFactor;
Expand Down Expand Up @@ -68,6 +69,8 @@ public void create() {
controlPointViews = new HashMap<>();
teamViews = new HashMap<>();
mm.addListener(this);
shutdownHook = new Thread(this::dispose);
Runtime.getRuntime().addShutdownHook(shutdownHook);
}

@Override
Expand Down Expand Up @@ -127,6 +130,11 @@ public void render() {

@Override
public void dispose() {
synchronized(shutdownHook) {
if (!Runtime.getRuntime().removeShutdownHook(shutdownHook)) {
return;
}
}
shapeRenderer.dispose();
batch.dispose();
font.dispose();
Expand Down

0 comments on commit bd8de47

Please sign in to comment.