The The java.lang.Runtime class is a subclass of the Object class and can provide various information about the program's running environment. The Java runtime environment creates a single instance of this class associated with the program. RuntimeThe class does not have any public constructor, so the program cannot create its own class instance. The program must call the getRuntime() method to obtain a reference to the current Runtime object. Important methods of the Runtime class include addShutdownHook(), exec(), exit(), freeMemory(), gc(), halt() and load(). The Chinese translation of
public class Runtime extends Object
public class RuntimeTest { static class Message extends Thread { public void run() { System.out.println(" Exit"); } } public static void main(String[] args) { try { Runtime.getRuntime().addShutdownHook(new Message()); System.out.println(" Program Started..."); System.out.println(" Wait for 5 seconds..."); Thread.sleep(5000); System.out.println(" Program Ended..."); } catch(Exception e) { e.printStackTrace(); } } }
Program Started... Wait for 5 seconds... Program Ended... Exit
The above is the detailed content of What is the importance of Runtime class in Java?. For more information, please follow other related articles on the PHP Chinese website!