You can try java.lang.Runtime.addShutdownHook () and intercept the hook across all threads and unload their stack traces using Thread.getAllStackTraces () . However, if the JVM was stopped at Runtime.halt() , then interceptors will not be called. More complicated would be to use instrumentation to connect to the calls of Runtime.exit() and Runtime.halt() (or Shutdown.sequence() , see Edit # 2), so you can see exactly what is happening while you are cause.
EDIT . Another way to do this is to install a SecurityManager that does not provide any protection, but which downloads a list of threads whenever SecurityManager.checkExit() is called, since both the halt() and exit() methods call this security manager method. This would be much simpler than using tools, and you would even decide to throw an exception in addition to registering what the threads are doing.
EDIT 2 . The JVM system is working, it can say that the JVM will end, and in this case, using the Security Manager will not work. Also, the hardware on Runtime.exit() or Runtime.halt() will not be used, since the method that is called is java.lang.Shutdown.exit() . And if the JVM shuts down because the last daemon thread has completed, then Shutdown.shutdown() called. But buttons work in any of these situations. . Therefore, you should always use shutdown hooks, even if you are also going to use a security manager or toolkit.
Matthew cline
source share