System 类 和 Runtime 类的常用用法

来源:互联网 发布:淘宝捉猫猫是什么 编辑:程序博客网 时间:2024/05/02 04:47

System类的常用用法

1,主要获取系统的环境变量信息

public static void sysProp()throws Exception{Map<String,String> env = System.getenv();//获取系统的所有环境变量for(String name : env.keySet()){System.out.println(name + " : " +env.get(name));}//获取系统的指定环境变量的值System.out.println(env.get("JAVA_HOME"));//获取系统的所有属性Properties prop = System.getProperties();//将系统的属性保存到配置文件中去prop.store(new FileOutputStream("Prop.properties"),"System properties");//输出特定的系统属性System.out.println(System.getProperty("os.name"));}


2,与系统时间有关的方法操作

public static void sysTime(){//获取系统当前的时间毫秒currentTimeMillis()(返回当前时刻距离UTC 1970.1.1 00:00的时间差)Long time = System.currentTimeMillis();System.out.println(time);Long time1 = System.nanoTime();//主要用于计算时间差单位纳秒Long time3 = System.currentTimeMillis();for(Long i =0l ;i <999l; i++){}Long time2 = System.nanoTime();Long time4 = System.currentTimeMillis();System.out.println(time2 - time1+ " : " +(time4 - time3));}


3,鉴别两个对象在堆内存当中是否是同一个

public static void identityHashCode(){//str1 str2为两个不同的String对象 String str1 = new String("helloWorld");String str2 = new String("helloWorld");//由于String类重写了hashCode()方法 所以 他们的HashCode是一样的System.out.println(str1.hashCode()+" : "+str2.hashCode());//由于他们不是同一个对象所以他们的计算出来的HashCode时不同的。//实际上该方法使用的时最原始的HashCode计算方法即Object的HashCode计算方法System.out.println(System.identityHashCode(str1) + " : "+ System.identityHashCode(str2));String str3 = "hello";String str4 = "hello";//由于他们引用的是常量池中的同一个对象 所以他们的HashCode是一样的System.out.println(System.identityHashCode(str3) + " : "+ System.identityHashCode(str4));/*输出如下-1554135584 : -155413558428705408 : 618231521648882 : 21648882*/}


Runtime类的常用用法

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

class RunTimeTest {public static void main(String[] args) throws Exception{getJvmInfo();//execTest();}public static void getJvmInfo(){//获取Java运行时相关的运行时对象Runtime rt = Runtime.getRuntime();System.out.println("处理器数量:" + rt.availableProcessors()+" byte");System.out.println("Jvm总内存数 :"+ rt.totalMemory()+" byte");System.out.println("Jvm空闲内存数: "+ rt.freeMemory()+" byte");System.out.println("Jvm可用最大内存数: "+ rt.maxMemory()+" byte");}public static void execTest()throws Exception{Runtime rt = Runtime.getRuntime();//在单独的进程中执行指定的字符串命令。 rt.exec("mspaint E:\\mmm.jpg");}}


 

1 0
原创粉丝点击