System Shutdown

System Shutdown

Is there an API that I can use to sense that the system is shutting down?

Up until just recently the answer was no. But Sun keeps on adding newAPIs to the ever-growing Java platform. The 1.3 version of the Java 2SDK adds the addShutdownHook(Thread hook) method to java.lang.Runtime.This method registers a thread to execute when the JVM shuts down.The JVM will shut itself down when your program exits normally or whenit is terminated by the operating system. Before exiting, the JVMwill execute all threads that have been registered withaddShutdownHook() in an unspecified order.

You should make sure thesethreads are short-lived and avoid deadlocks because the JVM may not beallowed too much time to shut down if terminated by an operating systemshutdown. There is no way to determine if a shutdown hook thread isbeing executed due to a normal program exit or an external event.However, if your Java program is a Unix daemon or NT service, it willonly exit due to an external event and a shutdown hook can assume thateither the operating system is shutting down or the program is beingexplicitly killed by the user.

See also - an AI reverse image search platform

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist