Skip to content

Instantly share code, notes, and snippets.

@nbhusare
Last active December 27, 2019 12:57
Show Gist options
  • Save nbhusare/126808b05cb7266bb0f690ac9b34fbdf to your computer and use it in GitHub Desktop.
Save nbhusare/126808b05cb7266bb0f690ac9b34fbdf to your computer and use it in GitHub Desktop.
Add a new Virtual Machine Shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
// Your code comes here...
}));
/* addShutdownHook() - A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its
shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the
hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine
will halt. Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if shutdown was
initiated by invoking the exitmethod. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment