java.lang.Runtime

      Runtime类封装了运行时的环境。每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。

      一般不能实例化一个Runtime对象,应用程序也不能创建自己的 Runtime 类实例,但可以通过 getRuntime 方法获取当前Runtime运行时对象的引用。

      一旦得到了一个当前的Runtime对象的引用,就可以调用Runtime对象的方法去控制Java虚拟机的状态和行为。

      当Applet和其他不被信任的代码调用任何Runtime方法时,常常会引起SecurityException异常。

package com.demo.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.concurrent.TimeUnit;

public class RuntimeDemo {

    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();

        //java虚拟机返回可用处理器的数目。
        System.out.println("avaliableProcessors = " + runtime.availableProcessors());

        //在单独的线程中执行指令,即使当前线程关闭,该指令的线程也不会关闭(即2线程无绑定)
        try {
//          Process process = runtime.exec("notepad");
//          Process process = runtime.exec("cmd /c dir");
            Process process = runtime.exec("cmd");
            System.out.println("process.isAlive() = " + process.isAlive());
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
            BufferedReader ebr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String line;
            bw.write("time /t");
            //Writes a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.
            bw.newLine();
            //实际写入并清空缓冲区
            bw.flush();
            bw.close();
            System.out.println("process inputStream : ");
            //注意由于readLine是阻塞函数,因此要等到OutputStream关闭再读,或者开一个线程去读
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }
            System.out.println("process errorStream : ");
            while((line = ebr.readLine()) != null) {
                System.out.println(line);
            }
            br.close();
            ebr.close();
            //导致当前进程等待1000ms,一直等到由该Process对象表示的进程已经终止
            try {
                process.waitFor(1000, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            process.destroy();
            System.out.println("after destroy, process.isAlive() = " + process.isAlive());
            System.out.println("process.exitValue() = " + process.exitValue());
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        System.out.println("free memory is " + runtime.freeMemory() + " / " + runtime.totalMemory() +  ", total is " + runtime.maxMemory());

        //当jvm关闭的时候,会执行系统中已经设置的所有通过方法addShutdownHook添加的钩子,当系统执行完这些钩子后,jvm才会关闭。所以这些钩子可以在jvm关闭的时候进行内存清理、对象销毁等操作。
        runtime.addShutdownHook(new Thread() {
            public void run() {
                try{
                    System.out.println("The JVM Hook is execute");
                    Thread.sleep(5000);
                    System.out.println("after 5000ms, The JVM Hook is execute");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值