Runtime
代表程序所在的运行环境
Runtime是一个单例类
Runtime类提供的常见方法
方法名 | 说明 |
---|---|
public static Runtime getRuntime() | 返回与当前Java应用程序关联的运行时对象 |
public void exit(int status) | 终止当前运行的虚拟机 |
public int availableProcessors() | 返回Java虚拟机可用的处理器数 |
public long totalMemory() | 返回Java虚拟机中的内存总量 |
public long freeMemory() | 返回Java虚拟机中的可用内存 |
public Process exec(String command) | 启动某个程序,并返回代表该程序的对象 |
案例演示
public class RuntimeTest {
public static void main(String[] args) throws Exception {
//public static Runtime getRuntime() 返回与当前Java应用程序关联的运行时对象
Runtime r = Runtime.getRuntime();
//public void exit(int status) 终止当前运行的虚拟机,status用作状态代码,非零状态代码表示异常终止
// r.exit(0);
//public int availableProcessors() 获取虚拟机能够使用的处理器数
System.out.println(r.availableProcessors());
//public long totalMemory() 返回Java虚拟机中的内存总量
System.out.println(r.totalMemory()); //单位:byte
//public long freeMemory() 返回Java虚拟机中的可用内存量
System.out.println(r.freeMemory()); //单位:byte
//public Process exec(String command) 启动某个程序,并返回代表该程序的对象
Process p = r.exec("\"D:\\QQ\\QQ.exe\""); //抛出异常:IOException
Thread.sleep(10000); //休眠10秒,抛出异常:InterruptedException
p.destroy(); //销毁进程,终止程序
//可抛出异常的直接父类Exception---throws Exception
}
}