1. Runtime類封裝了java虛擬機進程,一個虛擬機進程對應一個Runtime類實例對象.
2. 不能通過new來獲取Runtime的實例對象,通過getRuntime()獲取.獲取的對象實際上也是當前java的運行時環境.
3. 虛擬機進程本身是操作系統的一個進程,所以可以調用操作系統的執行程序,并注銷它.
4. Runtime中的大部分方法和System中是重復的.比如exit(x),用于退出JVM,實際上System中的exit(x)方法也是通過調用Runtime的exit(x)來實現的
程序清單:
public class RuntimeTest {
public static void main(String[] args) throws Exception{
Runtime rt = Runtime.getRuntime();
System.out.println("處理器數量:" + rt.availableProcessors());
System.out.println("空閑內存數:" + rt.freeMemory());
System.out.println("總內存數:" + rt.totalMemory());
System.out.println("可用最大內存數:" + rt.maxMemory());
// 打開記事本程序,等待5秒,關閉
Process process = rt.exec("notepad.exe");
Thread.sleep(5000);
process.destroy();
}
}