Runtime classes in Java

Source: Internet
Author: User

Runtime classes in Java

Each Java program has a single instance of one and only one runtime class.

The runtime class is very special and there is no construction method. It provides an interface to the application and Java. is to get an instance of the runtime class through Runtime.getruntime ().

The memory used by the JVM can be obtained through the object of the runtime class.

class Runtimetest

{

Public Static void Main (string[] args)

{

Runtime Rt=runtime. GetRuntime (); Static methods

System. out. println (Rt.freememory ()); Available memory

System.  out. println (Rt.totalmemory ()); Total Memory

}

}

Runtime can invoke external programs:

class Runtimetest

{

Public Static void Main (string[] args) {

Runtime Rt=runtime. GetRuntime (); Static methods

Try

{

Rt.Exec("notepad");

}

Catch (Exception e)

{

E.printstacktrace ();

}

}

}

The calling external program can return a Process,process object that can manage child processes. Process is an abstract class that cannot be instantiated directly, and is used to obtain an instance of this class through process p=rt.exec ("Javac Classtest.java").

import java.io.*;

class Runtimetest//In fact, this program is to print the Classtest.class input to the screen.

{

Public Static void Main (string[] args) {

Runtime Rt=runtime. GetRuntime (); Static methods

Try

{

Process p=rt.exec ("Java classtest");

InputStream Is=p.getinputstream ();

int data;

while ((Data=is.read ())!=-1)

{

System. out. Print ((char) data);

}

}

Catch (Exception e)

{

E.printstacktrace ();

}

}

}

Single-Case mode:

A class has only one instance and is instantiated by itself (not new, but in other ways, such as GetRuntime ()) and provides this instance to the entire system, a class called a singleton class. The object of the class is actually created inside the Singleton class or with new.

In order to avoid the external use of the construction method to the new instance Singleton class, the construction method of the class is declared private.

Runtime classes in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.