Runtime Class in Java
What it is?
Every Java application has a single instance of Runtime class that allows the application to interface with the environment in which the application is running. An application cannot create its own instance of Runtime class. So you can get an instance of this class by calling the getRuntime() method.
What we can do with this Runtime class, now?
Okay, before answering the above question, let’s go through class’s methods one by one. So here is a list of all methods available in this class —
1. public void exit(int status)
2. public void addShutdownHook(Thread hook)
3. public boolean removeShutdownHook(Thread hook)
4. public void halt(int status)
5. public static void runFinalizersOnExit(boolean value) @Deprecated
6. public Process exec(String command) throws IOException
7. public Process exec(String command, String[] envp) throws IOException
8. public Process exec(String command, String[] envp, File dir)
throws IOException
9. public Process exec(String cmdarray[]) throws IOException
10. public Process exec(String[] cmdarray, String[] envp)
throws IOException
11. public Process exec(String[] cmdarray, String[] envp, File dir)
throws IOException
12. public void runFinalization()
13. public void load(String filename) @CallerSensitive
14. public void loadLibrary(String libname) @CallerSensitive
There are some native methods also- We will talk about it also.
1. public native int availableProcessors();
2. public native long freeMemory();
3. public native long totalMemory();
4. public native long maxMemory();
5. public native void gc();
6. public native void traceInstructions(boolean on)
7. public native void traceMethodCalls(boolean on)
So, Let’s talk first about-
public static void runFinalizersOnExit(boolean value) @Deprecated
Enable or disable finalization on exit; doing so specifies that the finalizers of all objects that have finalizers that have not yet been automatically invoked are to be run before the Java runtime exits. By default, finalization on exit is disabled.
This method is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock so this method was deprecated.
public void addShutdownHook(Thread hook)
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 the shutdown was initiated by invoking the exit() method.
Once the shutdown sequence has begun it is impossible to register a new shutdown hook or de-register a previously-registered hook. Attempting either of these operations will cause an IllegalStateException to be thrown.
Once the shutdown sequence has begun it can be stopped only by invoking the halt() method, which forcibly terminates the virtual machine.
Shutdown hooks should also finish their work quickly. When a program invokes exit() the expectation is that the virtual machine will promptly shut down and exit. When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.
In rare circumstances the virtual machine may abort(), that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hook will be run.
public void exit(int status)
Terminates the currently running Java virtual machine by initiating its shutdown sequence. This method never returns normally. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
The virtual machine’s shutdown sequence consists of two phases.
In the first phase, all registered addShutdownHook() if any, are started in some unspecified order and allowed to run concurrently until they finish.
In the second phase, all uninvoked finalizers are run if runFinalizersOnExit() is enabled. Once this is done the virtual machine halts
If this method is invoked after the virtual machine has begun its shutdown sequence then if shutdown hooks are being run this method will block indefinitely. If shutdown hooks have already been run and on-exit finalization has been enabled then this method halts the virtual machine with the given status code if the status is nonzero; otherwise, it blocks indefinitely.
We will discuss the remaining methods in the next story.