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.