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

What is the importance of Runtime class in Java?

WBOY
Release: 2023-08-21 08:37:10
forward
628 people have browsed it

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
Copy after login

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

Output

Program Started...
Wait for 5 seconds...
Program Ended...
Exit
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template