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