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() {}
}

<mstyle mathcolor="&#35;4285f4"> g <mstyle mathcolor="&#35;ea4335"> o <mstyle mathcolor="&#35;fbbc05"> o <mstyle mathcolor="&#35;4285f4"> g <mstyle mathcolor="&#35;34a853"> l <mstyle mathcolor="&#35;ea4335"> e </mstyle> </mstyle> </mstyle> </mstyle> </mstyle> </mstyle> \color{#4285f4}{g}\color{#ea4335}{o}\color{#fbbc05}{o}\color{#4285f4}{g}\color{#34a853}{l}\color{#ea4335}{e} google

分析

  • <mark>先看构造</mark>:
    /** Don't let anyone else instantiate this class */
    private Runtime() {}

构造设为private,无法调用new对象。

  • <mark>在看如何获取对象</mark>:

用 public static 的方法 <mstyle mathcolor="&#35;ea4335"> g e t R u n t i m e ( ) </mstyle> \color{#ea4335}{getRuntime()} getRuntime(),可以通过静态调用获得对象。

/** * 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;
    }
  • <mark>最后,看单例的来源</mark>:
 private static Runtime currentRuntime = new Runtime();

在类初始化时候new一次对象,用 <mstyle mathcolor="&#35;4285f4"> c u r r e n t R u n t i m e </mstyle> \color{#4285f4}{currentRuntime} currentRuntime引用。
<mark>确保了,对象唯一,即“单例”</mark>
其中:
private 确保 <mstyle mathcolor="&#35;4285f4"> c u r r e n t R u n t i m e </mstyle> \color{#4285f4}{currentRuntime} currentRuntime不被随意修改。
static 确保能被 <mstyle mathcolor="&#35;ea4335"> g e t R u n t i m e ( ) </mstyle> \color{#ea4335}{getRuntime()} getRuntime()方法调用

饿汉式 or 懒汉式

上述方法为“饿汉式”,即在类初始化时候就 new 出唯一的对象。
还有一种“懒汉式”,即在调用 <mstyle mathcolor="&#35;ea4335"> g e t R u n t i m e ( ) </mstyle> \color{#ea4335}{getRuntime()} getRuntime()方法的时候才 new 出唯一的对象。
只需要稍微改下上面代码即可变为“懒汉式”。

public class Runtime {
    private static Runtime currentRuntime = null;

    /** * 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() {
    	if(currentRuntime == null){
    		currentRuntime = new Runtime();
    	}
        return currentRuntime;
    }

    /** Don't let anyone else instantiate this class */
    private Runtime() {}
}