Runtime

来源:互联网 发布:himall2.6官方版源码 编辑:程序博客网 时间:2024/06/05 04:53

Runtime类封装了运行时的环境。每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。Runtime类是单例的,只能通过getRuntime()方法获得Runtime对象。

常见应用:

  • 内存管理:

    Java提供了无用单元自动收集机制。通过totalMemory()和freeMemory()方法可以知道对象的堆内存有多大,还剩多少。Java会周期性的回收垃圾对象(未使用的对象),以便释放内存空间。但是如果想先于收集器的下一次指定周期来收集废弃的对象,可以通过调用gc()方法来根据需要运行无用单元收集器。举个例子来看一下:

package com.koma.demo;/** * @author koma * @version 2017年12月7日 下午5:08:39 */public class RuntimeTest {    public static void main(String[] args) {        memory();    }    public static void memory() {        Runtime r = Runtime.getRuntime();        System.out.println("totalMemory is :" + r.totalMemory());        System.out.println("freeMemory is : " + r.freeMemory());        r.gc();        long mem1 = r.freeMemory();               System.out.println("freeMemory after gc : " + mem1);        // 给数组分配内存        String someints[] = new String[100000];        for (int i = 0; i < 100000; i++)            someints[i] = i+"";        long mem2 = r.freeMemory();        System.out.println("freeMemory after allocation : " + mem2);        System.out.println("Memory used by allocation : " + (mem1 - mem2));        // 释放数组空间        for (int i = 0; i < 100000; i++)            someints[i] = null;        r.gc(); // request garbage collection        mem2 = r.freeMemory();        System.out.println("freeMemory after collecting discarded integers : " + mem2);    }}

结果:

totalMemory is128974848freeMemory is : 126930104freeMemory after gc : 128437504freeMemory after allocation : 107308104Memory used by allocation : 21129400freeMemory after collecting discarded integers : 128435928
  • addShutdownHook方法作用
    当程序正常退出,系统调用 System.exit方法或虚拟机被关闭时会执行添加的shutdownHook线程。其中shutdownHook是一个已初始化但并不有启动的线程,当jvm关闭的时候,会执行系统中已经设置的所有通过方法addShutdownHook添加的钩子,当系统执行完这些钩子后,jvm才会关闭。所以可通过这些钩子线程在jvm关闭的时候进行内存清理、资源回收等工作。
package com.koma.demo;/** * @author koma * @version 2017年12月8日 上午9:32:57 */public class HookTest {    public static void main(String[] args) {        Runtime.getRuntime().addShutdownHook(new HookThread());        try {            Thread.sleep(1000L);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println("主程序结束时间: "+System.currentTimeMillis());    }}class HookThread extends Thread{    @Override    public void run() {        System.out.println("hook run time: "+System.currentTimeMillis());    }}

结果:

主程序结束时间: 1512697007617hook run time: 1512697007629

钩子线程的时间晚于主程序结束时间。

  • 执行其他程序
    在安全的环境中,可以在多任务操作系统中使用Java去执行其他特别大的进程(也就是程序)。ecec()方法有几种形式命名想要运行的程序和它的输入参数。ecec()方法返回一个Process对象,可以使用这个对象控制Java程序与新运行的进程进行交互。ecec()方法本质是依赖于环境。
    在linux上用Java运行linux命令:
package com.koma.demo;import java.io.IOException;/** * @author koma * @version 2017年12月7日 下午5:08:39 */public class RuntimeTest {    public static void main(String[] args) {        processLinux();    }    public static void processLinux(){        Runtime r = Runtime.getRuntime();        try {            // 删除test.txt文件            Process p=r.exec("rm -rf test.txt");            // 等待删除完成,删除完成后返回0            int i=p.waitFor();            System.out.println("删除完成后返回: "+i);        } catch (IOException | InterruptedException e) {            e.printStackTrace();        }    }}

在windows上调用记事本程序。

package com.koma.demo;/** * @author koma * @version 2017年12月7日 下午5:08:39 */public class RuntimeTest {    public static void main(String[] args) {        processWin();    }    public static void processWin() {        Runtime r = Runtime.getRuntime();        Process p = null;        try {            p = r.exec("notepad");            // 会阻塞,等待进程结束            p.waitFor();            System.out.println("阻塞了吗");        } catch (Exception e) {            System.out.println("Error executing notepad.");        }        System.out.println("Notepad returned " + p.exitValue());    }}
原创粉丝点击