I'm attempting to get my Java application to close, but threads are being left open.
When close is clicked using the default windows x button, everything shuts fine (probably due to EXIT_ON_CLOSE?)- but when I use a programmatic button, it hangs on thread.join().
Even worse, the window is disposed fine, so the user would think it's shut- yet there are several AWT threads that stay open. My main thread is waiting on a thread with an id of 20, but I have no idea how to get thread IDs.
Does anyone have any suggestions?
Here's my exit code:
public synchronized void stop() {
running = false;
frame.dispose();
WindowEvent wev = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
try {
if (server != null) {
server.exit();
}
client.exit();
thread.join();
new Thread(){
public void run() {
System.exit(0);
}
}.start();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
System.exit(1);
}
}
and here are the threads open post-exit:
and here's the contents of my run()
method:
public void run() {
requestFocus();
while (running) {
getTimer().tick();
if (System.currentTimeMillis() - getTimer().getSecond() > 1000) {
// every second, add a second, print fps and mod title with fps
getTimer().accumulateSecond();
//System.out.println(getTimer().returnFPS());
frame.setTitle(title + " | " + getTimer().returnFPS());
getTimer().resetTick();
ticker++;
}
while (getTimer().getDelta() >= 1) {
// every time delta goes greater than one, update and supertick
update();
getTimer().superTick();
}
if (getTimer().getFPS() > 100) {
try {
Thread.sleep(5);
} catch (Exception e) {
System.err.println("Sleeping failed: " + e);
}
}
render();
if (ticker > 30) {
ticker = 0;
getTimer().hourTick();
}
}
stop();
}