System类 和 Runtime 类
java程序在不同操作系统上运行时,可能需要取得平台相关属性,或者调用平台本地命令(如windows下sys32和system64下的可执行文件、本地其他语言写的函数等) 来完成特定功能.java提供了System和Runtime两个类来与程序的运行平台交互。
1.System类
首先,看构造器(constructor),是一个私有的构造器,这里注释写明java程序不能创建System对象,因静态代码块里面定义了一个nativ方法registerNatives(),代码运行时,需依赖虚拟机的相关参数来初始化该类的实例;
public final class System { /* register the natives via the static initializer. * * VM will invoke the initializeSystemClass method to complete * the initialization for this class separated from clinit. * Note that to use properties set by the VM, see the constraints * described in the initializeSystemClass method. */ private static native void registerNatives(); static { registerNatives(); } /** Don't let anyone instantiate this class */ private System() { }
其次Syetem类提供了代表标准输入、标准输出和错误输出的类成员,这里的System.in 和System.out,在通过Scanner类可进行人工程序输入互动;
最后System类还提供了一些静态方法用于访问环境变量、系统属性的方法(这里列举一些认为常用的,具体可以参考该类的API);
package based.libraries; import java.util.Map; /** * * @author fan *2018年8月9日 */ public class SystemTest { public static void main(String[] args) { /** * 通知系统进行垃圾回收 */ System.gc(); System.runFinalization(); /** * 当前毫秒数 */ System.out.println(System.currentTimeMillis());
/**
* 加载 c写的AuCpuCardForJava.dll,之后可以定义java 的nativ类型的方法,执行效率较高
*/
System.loadLibrary("AuCpuCardForJava");
/** * 准确计算任意对象的hashCode值,如果hashCode()有重写,此处的hashCode唯一标注该对象实例 */ System.out.println(System.identityHashCode(System.in)); /** * 获取系统所有运行环境 */ Map<String,String> envs = System.getenv(); for (String key : envs.keySet()) { System.out.println(key+"------------------>"+envs.get(key)); } System.out.println("-------------------分割线------------------------"); /** * 获取指定环境变量的value */ System.out.println(System.getenv("JAVA_HOME")); System.out.println("-------------------分割线------------------------"); /** * 获取所有系统属性 */ System.out.println(System.getProperties()); } }
2.Runtime类
Runtime类描述Java运行时环境,即每一个程序都有一个对应的Runtime实例,应用程序通过该对象和与运行环境关联;
构造器与Sytem相同都是私有的,但多了一个public 的getRuntime()方法获取实例--单例模式(思考下这里为什么没判断为null?因为没必要!);
public class Runtime { private static Runtime currentRuntime = new Runtime(); /** * Returns the runtime object associated with the current Java application. * Most of the methods of class <code>Runtime</code> are instance * methods and must be invoked with respect to the current runtime object. * * @return the <code>Runtime</code> object associated with the current * Java application. */ public static Runtime getRuntime() { return currentRuntime; } /** Don't let anyone else instantiate this class */ private Runtime() {}
与System类 类似,Runtime也定义了一些静态方法来与运行环境互通,和访问相关参数。多说一句,System类的gc(),runFinalization(),loadLibrary()其实都是间接调用了Runtime对应的方法,具体详见源码。
package based.libraries; import java.io.IOException; public class RuntimeTest { public static void main(String[] args) throws IOException { Runtime rt = Runtime.getRuntime(); System.out.println("处理器数量"+rt.availableProcessors()); System.out.println("空闲内存"+rt.freeMemory()); System.out.println("总内存"+rt.totalMemory()); System.out.println("可用最大内存"+rt.maxMemory()); String str =""; Object obj =new Object(); RuntimeTest rts = new RuntimeTest(); } }