JAVA API(二)System类与Runtime类

1.System类与Runtime类

1.1System类

System类对我们来说并不陌生,在之前学习的知识中,当我们需要打印结果时,使用的都是"System.out.println()"语句进行打印输出,这句代码中就使用了System类。这个类中定义了与系统相关的属性和方法,它所提供的属性和方法都是静态的,因此,想要引用这些属性和方法,直接使用System类调用即可。下表是System类常用的一些方法。

方法声明 功能描述
static void exit(int status) 该方法用于终止当前正在运行的Java虚拟机,其中,参数status表示状态吗,若状态码非0,则表示异常终止
static void gc() 运行垃圾回收器,并对垃圾进行回收
static native long currentTimeMillis(); 返回以毫秒为单位的当前时间
static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length) 从src引用的指定源数组赋值到dest引用的数组,赋值从指定的位置开始,到目标数组的指定位置结束,会覆盖目标数组中对应位置的元素
static Properties getProperties() 取得当前的系统属性
static String getProperty(String key) 获取指定键描述的系统属性

 

 

 

 

 

 

  •  getProperties()方法

  getProperties()方法用于获取当前系统的全部属性,该方法会返回一个Properties对象,其中封装了系统的所有属性,这些属性是以键值对形式存在的。

  Example01.java  

public class Example01 {
    public static void main(String[] args) {
        //获取当前系统的属性
        Properties properties = System.getProperties();
        System.out.println(properties);
        //获取所有系统属性的key(属性名),返回Set对象
        Set<String> propertyNames = properties.stringPropertyNames();
        for (String key : propertyNames) {
            //获取当前键(key)对应的值(属性值)
            String value = System.getProperty(key);
            System.out.println(key + "---->" + value);
        }
    }
}

运行结果如下:

  • currentTimeMillis()方法

  该方法返回一个long类型的值,该值表示当前时间与1970年1月1日0点0时0分0秒之间的时间差,单位是毫秒,通常也将该值称为时间戳。

Example02.java

public class Example02 {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();//循环开始的当前时间
        int sum = 0;
        for (int i = 0; i < 100000000; i++) {
            sum += i;
        }
        long endTime = System.currentTimeMillis();//循环结束后的当前时间
        System.out.println("程序运行的时间为:" + (endTime - startTime) + "毫秒");
    }
}

  运行结果如下:

  • arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

  arraycopy()方法用于将一个数组中的元素快速拷贝到另一个数组,其中参数的具体作用如下:

  src:表示源数组。

  dest:表示目标数组。

  srcPost:表示源数组中拷贝元素的起始位置。

  destPos:表示拷贝到目标数组的起始位置。

  length:表示拷贝元素的个数。

  需要注意的是,在进行数组复制时,源数组中必须有足够拷贝个数的元素存在,同时目标数组必须有足够的空间来存放拷贝的元素,否则会发生角标越界异常。

 Example03.java

public class Example03 {
    public static void main(String[] args) {
        int[] fromArray = {101,102,103,104,105,106};//源数组
        int[] toArray = {201,202,203,204,205,206,207};//目标数组
        System.arraycopy(fromArray, 2, toArray, 3, 4);
        //打印目标数组中的元素
        for (int i = 0; i < toArray.length; i++) {
            System.out.println(i + ":" + toArray[i]);
        }
    }
}

 

 运行结果如下:

 

除了上面例子中的方法外,System类还有两个常见的方法,分别是gc()和exit(int status)方法。其中,gc()方法用来启动Java的垃圾回收器,并且对内存中的垃圾对象进行回收。exit(int status)方法用来终止当前正在运行的Java虚拟机,其中的参数status用于表示当前发生的异常状态,通常指定为0,表示正常退出,否则表示异常终止。

 

1.2Runtime类

 Runtime类表示虚拟机运行时的状态,用于封装虚拟机进程。每次使用Java命令启动虚拟机都对应一个Runtime实例,并且只有一个实例,因此该类采用单例模式进行设计,对象不可以直接实例化。若想在程序中获取一个Runtime实例,只能通过以下方法:

Runtime run = Runtime.getRuntime();

 

由于Runtime实例中封装了虚拟机进程,因此在程序中通常会通过该类的实例对象来获取当前虚拟机的相关信息。

Example04.java

public class Example04 {
    public static void main(String[] args) {
        Runtime rt = Runtime.getRuntime();//获取Runtime实例
        System.out.println("处理器的个数:" + rt.availableProcessors() + "个");
        System.out.println("空闲内存数量:" + rt.freeMemory()/1024/1024 + "M");
        System.out.println("最大可用内存数量:" + rt.maxMemory()/1024/1024 + "M");
    }
}

 运行结果如下:

在上面的例子中,availableProcessors()方法,freeMemory()方法和maxMemory()方法分别计算当前虚拟机的处理器个数,空闲内存数和最大内存数量,单位是字节。

Runtime类提供了一个exec()方法,该方法用于执行一个dos命令,从而实现与在命令行窗口中输入dos命令同样的效果,例子如下:

Example05.java

public class Example05 {
    public static void main(String[] args) throws IOException {
        Runtime rt = Runtime.getRuntime();//创建Runtime实例对象
        rt.exec("notepad.exe");//调用exec()方法
    }
}

 

 Runtime类的exec()方法会返回一个Process对象,该对象表示操作系统的一个进程,通过该对象可以对产生的新进程进行管理,如需要关闭进程 只需要调用destroy()方法即可。

Example06.java

public class Example06 {
    public static void main(String[] args) throws IOException, InterruptedException {
        Runtime rt = Runtime.getRuntime();//创建Runtime实例对象
        Process process = rt.exec("notepad.exe");//调用exec()方法
        Thread.sleep(3000);//程序休眠3秒
        process.destroy();//杀掉进程
    }
}

 

posted @ 2017-05-29 21:32  gouyadong  阅读(581)  评论(0编辑  收藏  举报