I've seen a question here asked by @Tej Kiran which is exactly my question but it is not answered, the last comment says:
"Do you know if there are any shutdown hooks registered in your application, or if any of the libraries you are using that has a shutdown hook? The shutdown hook is a thread, and if there is a deadlock in that thread causing it to never terminate, the JVM will never exit."
There is a method shutdown hook in my program
Runtime.getRuntime().addShutdownHook(new Thread("Shutdown Hook") {
@Override
public void run() {
System.out.println("---------------");
System.out.printf("%d threads running%n", Thread.activeCount());
Map<Thread, StackTraceElement[]> threads = Thread
.getAllStackTraces();
for (Entry<Thread, StackTraceElement[]> e : threads.entrySet()) {
Thread t = e.getKey();
System.out.printf("%s\t%s\t%s%n", t.getName(),
t.isDaemon(), t.isAlive());
StackTraceElement[] elements = e.getValue();
for (StackTraceElement trc : elements) {
System.out.println("\t" + trc);
}
}
System.out.println("---------------");
try {UIUtil.cancelAllTasks();} catch (Throwable e) {e.printStackTrace();};
try {mehad.io.port.ScannerManager.disableAutoHandshake();} catch (Throwable e) {e.printStackTrace();};
try {mehad.io.port.ComPortInterface.getInstance().close();} catch (Throwable e) {e.printStackTrace();};
try {
if (lockStream != null) {
lockStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
But I don't know how to distinguish if there is a deadlock in my shutdown hook and if there is any, how to solve it.
My own answer:
you know it seems that it has nothing to do with shutdown hook! I'd used WindowListener to close the MainFrame, I added a condition if to show a message whether the user want to leave or not? and if the answer was yes it goes to System.exit(0). previously there wasn't the condition to ask before closing the window. And Now it works fine! even on some rare machines which didn't work before. But Still I don't know how it works fine with that line added! I would be grateful if anyone can explain how it works fine now!