Runtime

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
    }
}

相关推荐

  1. <span style='color:red;'>Runtime</span>

    Runtime

    2024-02-20 10:20:02      41 阅读
  2. Runtime

    2024-02-20 10:20:02       50 阅读
  3. C#-NGWS runtime

    2024-02-20 10:20:02       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-20 10:20:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-20 10:20:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-20 10:20:02       87 阅读
  4. Python语言-面向对象

    2024-02-20 10:20:02       96 阅读

热门阅读

  1. 头歌:共享单车之数据分析

    2024-02-20 10:20:02       50 阅读
  2. 每天一个数据分析题(一百五十九)

    2024-02-20 10:20:02       64 阅读
  3. 23种设计模式-Golang(完整版)

    2024-02-20 10:20:02       43 阅读
  4. ubuntu18 环境安装

    2024-02-20 10:20:02       49 阅读