File size: 1,828 Bytes
0284885 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package Runtime_package;
import java.io.IOException;
public class Runtime_demo1 {
public static void main(String[] args) throws IOException {
/*
public static Runtime getRuntime() 当前系统的运行环境对象
public void exit(int status) 停止虚拟机
public int availableProcessors() 获得CPU的线程数
public long maxMemory() JVM能从系统中获取总内存大小(单位byte)
public long totalMemory() JVM已经从系统中获取总内存大小(单位byte)
public long freeMemory() JVM剩余内存大小(单位byte)
public Process exec(String command) 运行cmd命令
*/
//1、获取Runtime的对象
Runtime r1 = Runtime.getRuntime();
Runtime r2 = Runtime.getRuntime();
System.out.println(r1 == r2);//true --- 只能获取一个Runtime对象
//2、exit 停止虚拟机
// r1.exit(0);
// System.out.println("看看我执行了吗?");
//3、获取CPU的线程数
System.out.println(Runtime.getRuntime().availableProcessors());
//4、总内存大小
System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024);
//5、已经获取的总内存大小
System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024);
//6、剩余内存大小
System.out.println(Runtime.getRuntime().freeMemory() / 1024 / 1024);
//7、运行cmd命令
//shutdown:关机
//加上参数才能执行
//-s:默认在1分钟之后关机
//-s -t 指定时间:指定关机时间
//-a:取消关机操作
//-r:关机并重启
Runtime.getRuntime().exec("shutdown -a");
}
}
|