JVM shutting down
In this chapter you will learn:
Exit and halt Java virtual machine
void exit(int status)
Terminates the currently running Java virtual machine by initiating its shutdown sequence.void halt(int status)
Terminates the currently running Java virtual machine.
public class Main {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
/* j a va2 s .c o m*/
runtime.exit(2);
}
}
Shut down hook
We can add to do list for JVM shutting down. For example we can close resource or write to log file when the JVM is shutting down.
void addShutdownHook(Thread hook)
Registers a new virtual-machine shutdown hook.boolean removeShutdownHook(Thread hook)
De-registers a previously-registered virtual-machine shutdown hook.
public class Main {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
//from j a v a2s.c o m
runtime.addShutdownHook(new Thread(){
public void run(){
System.out.println("Shut down");
}
});
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Utility Classes