Java Runtime

180 阅读1分钟

Runtime

Runtime表示当前虚拟机的运行环境

方法名说明
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命令
  • 在Runtime底层代码中私有化了构造方法private Runtime() {},无法在外界创建Runtime对象

  • 但在里面又提供了private static final Runtime currentRuntime = new Runtime()在类中创建了一个唯一对象

  • 并通过public static Runtime getRuntime()方法进行了返回该对象

  • 这样设计的好处:在外界不管在哪一个类中调用public static Runtime getRuntime()方法,获取到的都是同一个对象

  • cmd命令

    • shutdown:关机(加上参数才能运行)
      • -s:默认在1分钟后关机
      • -s -t 指定时间:指定关机时间
      • -a:取消关机操作
      • -r:关机并重启