JDK源碼中單例模式的應用
1、Runtime類
Runtime類封裝了Java運行時的環境。每一個java程序實際上都是啟動了一個JVM進程,那么每個JVM進程都是對應這一個Runtime實例,此實例是由JVM為其實例化的。每個 Java 應用程序都有一個 Runtime 類實例,使應用程序能夠與其運行的環境相連接。
由於Java是單進程的,所以,在一個JVM中,Runtime的實例應該只有一個。所以應該使用單例來實現。

1 public class Runtime { 2 private static Runtime currentRuntime = new Runtime(); 3
4 public static Runtime getRuntime() { 5 return currentRuntime; 6 } 7 private Runtime() {} 8 }
以上代碼為JDK中Runtime類的部分實現,是餓漢式單例模式。在該類第一次被classloader加載的時候,實例就被創建出來了。一般不能實例化一個Runtime對象,應用程序也不能創建自己的 Runtime 類實例,但可以通過 getRuntime 方法獲取當前Runtime運行時對象的引用。
驗證:

1 Runtime r1 = Runtime.getRuntime(); 2 Runtime r2 = Runtime.getRuntime(); 3 System.out.println(r1 == r2);
運行結果:
true
2、java.awt.Toolkit#getDefaultToolkit()
懶漢式單例。不需要事先創建好,只要在第一次真正用到的時候再創建就可以了。因為很多時候並不常用Java的GUI和其中的對象。如果使用餓漢單例的話會影響JVM的啟動速度。

1 public abstract class Toolkit { 2
3 private static Toolkit toolkit; 4
5 public static synchronized Toolkit getDefaultToolkit() { 6 if (toolkit == null) { 7 java.security.AccessController.doPrivileged( 8 new java.security.PrivilegedAction<Void>() { 9 public Void run() { 10 Class<?> cls = null; 11 String nm = System.getProperty("awt.toolkit"); 12 try { 13 cls = Class.forName(nm); 14 } catch (ClassNotFoundException e) { 15 ClassLoader cl = ClassLoader.getSystemClassLoader(); 16 if (cl != null) { 17 try { 18 cls = cl.loadClass(nm); 19 } catch (final ClassNotFoundException ignored) { 20 throw new AWTError("Toolkit not found: " + nm); 21 } 22 } 23 } 24 try { 25 if (cls != null) { 26 toolkit = (Toolkit)cls.newInstance(); 27 if (GraphicsEnvironment.isHeadless()) { 28 toolkit = new HeadlessToolkit(toolkit); 29 } 30 } 31 } catch (final InstantiationException ignored) { 32 throw new AWTError("Could not instantiate Toolkit: " + nm); 33 } catch (final IllegalAccessException ignored) { 34 throw new AWTError("Could not access Toolkit: " + nm); 35 } 36 return null; 37 } 38 }); 39 loadAssistiveTechnologies(); 40 } 41 return toolkit; 42 } 43 }
以上代碼是Toolkit類的單例實現。這里類加載時只靜態聲明了私有toolkit並沒有創建Toolkit實例對象,延遲加載加快了JVM啟動速度。單例模式作為一種創建模式,在依賴加載的時候應用了另一種創建對象的方式,不是new新的對象,因為Toolkit本身是個抽象類不能實例化對象,而是通過反射機制加載類並創建新的實例。
3、java.awt.GraphicsEnvironment#getLocalGraphicsEnvironment()

1 public abstract class GraphicsEnvironment { 2 private static GraphicsEnvironment localEnv; 3 public static synchronized GraphicsEnvironment getLocalGraphicsEnvironment() { 4 if (localEnv == null) { 5 localEnv = createGE(); 6 } 7 return localEnv; 8 } 9
10 private static GraphicsEnvironment createGE() { 11 GraphicsEnvironment ge; 12 String nm = AccessController.doPrivileged(new GetPropertyAction("java.awt.graphicsenv", null)); 13 try { 14 Class<GraphicsEnvironment> geCls; 15 try { 16 geCls = (Class<GraphicsEnvironment>)Class.forName(nm); 17 } catch (ClassNotFoundException ex) { 18 ClassLoader cl = ClassLoader.getSystemClassLoader(); 19 geCls = (Class<GraphicsEnvironment>)Class.forName(nm, true, cl); 20 } 21 ge = geCls.newInstance(); 22 if (isHeadless()) { 23 ge = new HeadlessGraphicsEnvironment(ge); 24 } 25 } catch (ClassNotFoundException e) { 26 throw new Error("Could not find class: "+nm); 27 } catch (InstantiationException e) { 28 throw new Error("Could not instantiate Graphics Environment: "
29 + nm); 30 } catch (IllegalAccessException e) { 31 throw new Error ("Could not access Graphics Environment: "
32 + nm); 33 } 34 return ge; 35 } 36 }
這里類加載時只靜態聲明了私有localEnv並沒有創建實例對象。在GraphicsEnvironment類被第一次調用時會創建該對象。這里的createGE()方法也是通過反射的方式創建對象的。
總結:
(1)當一個類的對象只需要或者只可能有一個時,應該考慮單例模式。
(2)如果一個類的實例應該在JVM初始化時被創建出來,應該考慮使用餓漢式。
(3)如果一個類的實例不需要預先被創建,也許這個類的實例並不一定能用得上,也許這個類的實例創建過程比較耗費時間,也許就是真的沒必要提前創建。那么應該考慮懶漢式。
(4)在使用懶漢式單例的時候,應該考慮到線程的安全性問題。