JDK源码解析-Runtime类
生活随笔
收集整理的這篇文章主要介紹了
JDK源码解析-Runtime类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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類使用的是餓漢式(靜態(tài)屬性)方式來實現(xiàn)單例模式的。
使用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()); ?//創(chuàng)建一個新的進程執(zhí)行指定的字符串命令,返回進程對象Process process = runtime.exec("ipconfig");//獲取命令執(zhí)行后的結果,通過輸入流獲取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类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单例模式存在的问题——破坏单例模式,序列
- 下一篇: 工厂模式概述