JAVA API(二)System类与Runtime类

1.System类与Runtime类

1.1System类

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

方法声明 功能描述
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);
        }
    }
}

运行结果以下:spa

  • 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()方法用于将一个数组中的元素快速拷贝到另外一个数组,其中参数的具体做用以下:3d

  src:表示源数组。code

  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();//杀掉进程
    }
}
相关文章
相关标签/搜索