JDK源码解析-Runtime类
生活随笔
收集整理的这篇文章主要介绍了
JDK源码解析-Runtime类
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
Runtime类就是使用的单例设计模式
通过源代码查看使用的是哪儿种单例模式
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() {}... }从上面源代码中可以看出Runtime类使用的是饿汉式(静态属性)方式来实现单例模式的。
使用Runtime类中的方法
public class RuntimeDemo {public static void main(String[] args) throws IOException {//获取Runtime类对象Runtime runtime = Runtime.getRuntime(); //返回 Java 虚拟机中的内存总量。System.out.println(runtime.totalMemory());//返回 Java 虚拟机试图使用的最大内存量。System.out.println(runtime.maxMemory()); //创建一个新的进程执行指定的字符串命令,返回进程对象Process process = runtime.exec("ipconfig");//获取命令执行后的结果,通过输入流获取InputStream inputStream = process.getInputStream();byte[] arr = new byte[1024 * 1024* 100];int b = inputStream.read(arr);System.out.println(new String(arr,0,b,"gbk"));} }
总结
以上是生活随笔为你收集整理的JDK源码解析-Runtime类的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 单例模式存在的问题——破坏单例模式,序列
- 下一篇: 工厂模式概述