Home  >  Article  >  Java  >  What is the importance of Runtime class in Java?

What is the importance of Runtime class in Java?

WBOY
WBOYforward
2023-08-21 08:37:10592browse

What is the importance of Runtime class in Java?

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

Syntax

public class Runtime extends Object

Example

is:

Example

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();
      }
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete