java的System类和Runtime类

System类中的常用的一些方法

-----------------------------------------

arraycopy( )方法,这个方法需要传入5个参数:

第一个参数:需要拷贝的对象数组

第二个参数:从第几个下标开始

第三个参数:接收覆盖的对象数组

第四个参数:从第几个下标开始覆盖

第五个参数:需要拷贝覆盖多长呢


这个内存数组拷贝,比用循环的速度要快一般以上,数据越大效果越明显。来个小小的实验↓

for循环拷贝,拷贝100次,需要3151毫秒


System.arraycopy方法,也是重复拷贝100次,需要2137毫秒


-----------------------------------------

getenv( )方法,构造器有空参数的,和一个String参数的。

空参数的,拿出全部环境变量信息 集合对象,里面存放着所有的环境变量信息。返回Map<String,String>集合

String参数的,拿出指定名称的环境变量信息


-----------------------------------------

getProperties( ) 方法用来得到系统参数


-----------------------------------------

loadLibrary( ) 加载第三方库

-----------------------------------------

getLogger( ) 日志输出,jdk1.9 新功能

System.Logger log=System.getLogger("aaa");

log.log(System.Logger.Level.INFO,"asdasdasdasd");
log.log(System.Logger.Level.WARNING,"asdasdasdasd");
log.log(System.Logger.Level.ERROR,"asdasdasdasd");
-----------------------------------------

exit( ) 关闭虚拟机,需要传入一个int参数,0表示正常关闭虚拟机,1或者非0的数表示非正常关闭。


-----------------------------------------

Runtime类

这个Runtime类的单例的,而且是饿汉式单例。


所以得到对象用Runtime.getRuntime( );来获得。

Runtime常用的方法:

gc( ) 促进垃圾回收方法(只是促进作用,并不是马上就可以回收掉);

exit( ) 关闭虚拟机方法,上面的System类的System.exit( )方法,实际上就是调用了Runtime的exit方法。看看System.exit( )方法的源码就知道了。


exec( ) 方法


Windows下简单的用Runtime.exec来调用系统命令来打开一个网页试试:

public static void main(String[] args) {
        Runtime runtime=Runtime.getRuntime();
        String [] cmd={"cmd","/C","start https://www.baidu.com/"};
        try {
            Process proc=runtime.exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_40550973/article/details/80593378