System类 和 Runtime 类

java程序在不同操作系统上运行时,可能需要取得平台相关属性,或者调用平台本地命令(如windows下sys32和system64下的可执行文件、本地其他语言写的函数等) 来完成特定功能.java提供了System和Runtime两个类来与程序的运行平台交互。

1.System类

首先,看构造器(constructor),是一个私有的构造器,这里注释写明java程序不能创建System对象,因静态代码块里面定义了一个nativ方法registerNatives(),代码运行时,需依赖虚拟机的相关参数来初始化该类的实例;

1public final class System { 2 3 /* register the natives via the static initializer. 4 * 5 * VM will invoke the initializeSystemClass method to complete 6 * the initialization for this class separated from clinit. 7 * Note that to use properties set by the VM, see the constraints 8 * described in the initializeSystemClass method. 9 */ 10 private static native void registerNatives(); 11 static { 12 registerNatives(); 13 } 14 15 /** Don't let anyone instantiate this class */ 16 private System() { 17 }

其次Syetem类提供了代表标准输入、标准输出和错误输出的类成员,这里的System.in 和System.out,在通过Scanner类可进行人工程序输入互动;

最后System类还提供了一些静态方法用于访问环境变量、系统属性的方法(这里列举一些认为常用的,具体可以参考该类的API);

1package based.libraries; 2 3import java.util.Map; 4 5/** 6 * 7 * @author fan 8 *2018年8月9日 9 */ 10public class SystemTest { 11 12 public static void main(String[] args) { 13 /** 14 * 通知系统进行垃圾回收 15 */ 16 System.gc(); 17 System.runFinalization(); 18 /** 19 * 当前毫秒数 20 */ 21 System.out.println(System.currentTimeMillis());          

          /**
          * 加载 c写的AuCpuCardForJava.dll,之后可以定义java 的nativ类型的方法,执行效率较高
          */
          System.loadLibrary("AuCpuCardForJava");

1/** 2 * 准确计算任意对象的hashCode值,如果hashCode()有重写,此处的hashCode唯一标注该对象实例 3 */ 4 System.out.println(System.identityHashCode(System.in)); 5 /** 6 * 获取系统所有运行环境 7 */ 8 Map<String,String> envs = System.getenv(); 9 for (String key : envs.keySet()) { 10 System.out.println(key+"------------------>"+envs.get(key)); 11 } 12 System.out.println("-------------------分割线------------------------"); 13 /** 14 * 获取指定环境变量的value 15 */ 16 System.out.println(System.getenv("JAVA_HOME")); 17 System.out.println("-------------------分割线------------------------"); 18 /** 19 * 获取所有系统属性 20 */ 21 System.out.println(System.getProperties()); 22 23 } 24 25}

2.Runtime类

Runtime类描述Java运行时环境,即每一个程序都有一个对应的Runtime实例,应用程序通过该对象和与运行环境关联;

构造器与Sytem相同都是私有的,但多了一个public 的getRuntime()方法获取实例--单例模式(思考下这里为什么没判断为null?因为没必要!);

1public class Runtime { 2 private static Runtime currentRuntime = new Runtime(); 3 4 /** 5 * Returns the runtime object associated with the current Java application. 6 * Most of the methods of class <code>Runtime</code> are instance 7 * methods and must be invoked with respect to the current runtime object. 8 * 9 * @return the <code>Runtime</code> object associated with the current 10 * Java application. 11 */ 12 public static Runtime getRuntime() { 13 return currentRuntime; 14 } 15 16 /** Don't let anyone else instantiate this class */ 17 private Runtime() {}

   与System类 类似,Runtime也定义了一些静态方法来与运行环境互通,和访问相关参数。多说一句,System类的gc(),runFinalization(),loadLibrary()其实都是间接调用了Runtime对应的方法,具体详见源码。

1package based.libraries; 2 3import java.io.IOException; 4 5public class RuntimeTest { 6 7 public static void main(String[] args) throws IOException { 8 9 10 11 Runtime rt = Runtime.getRuntime(); 12 System.out.println("处理器数量"+rt.availableProcessors()); 13 System.out.println("空闲内存"+rt.freeMemory()); 14 System.out.println("总内存"+rt.totalMemory()); 15 System.out.println("可用最大内存"+rt.maxMemory()); 16 17 String str =""; 18 Object obj =new Object(); 19 RuntimeTest rts = new RuntimeTest(); 20 21 22 } 23 24 25}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java日期时间API系列31

  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前