How to prevent shutdown lock due to deadlock?

I saw the question here asked by @Tej Kiran, which is exactly my question, but did not answer it, the last comment says:

β€œDo you know if there are any stop hooks registered in your application, or if any of the libraries that you use has a shutdown hook? A shutdown hook is a thread, and if there is a dead end in this thread that forces it never run out, the JVM will never go out. "

My program has a shutdown shutdown method

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 do not know how to distinguish whether there is a dead end in my hook turned off, and if there is one, how to solve it.

+5
source share
2 answers

To start, your ShutdownHook works great. Now about the dead ends.

Do you know if there are any stop hooks registered in your application?

I am sure that there are no such things, because if in some third part there is another dead end, your application will be blocked even without your hook. I believe that this is not a fact, because you are complaining about your code, so without your hook it works fine and therefore there are no third-party hooks.

Your dead end is in one of these calls:

 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(); } 

The fact that they are wrapped in a try / catch block does not prevent them from being stuck.

There is no code in these methods. Even lockStream.close (); may block execution.

Remove them one of mine and see what causes the lock. Then go inside and see what actually blocks them.

0
source

My own answer:

you know that this has nothing to do with shutting down! I used WindowListener to close the MainFrame, I added a condition if I see a message about whether the user wants to leave or not? and if the answer was yes, it goes to System.exit (0). previously there was no condition to ask before closing the window. And now everything works fine! even on some rare machines that didn't work before. But still, I don’t know how this works great with this line! I would appreciate it if anyone could explain how this works now!

  this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { try { ComPortInterface.getInstance().close(); } catch (Exception e) { e.printStackTrace(); } //this if condition is added to ask if you want to exit or not if (MessageUtil.showYesNo("Exit ?")) { System.exit(0); } // System.exit(0); } }); 
0
source

All Articles