java源码单例模式_JDK源码——单例模式

JDK源码中单例模式的应用

1、Runtime类

Runtime类封装了Java运行时的环境。每一个java程序实际上都是启动了一个JVM进程,那么每个JVM进程都是对应这一个Runtime实例,此实例是由JVM为其实例化的。每个

Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。

由于Java是单进程的,所以,在一个JVM中,Runtime的实例应该只有一个。所以应该使用单例来实现。

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 public classRuntime {2 private static Runtime currentRuntime = newRuntime();3

4 public staticRuntime getRuntime() {5 returncurrentRuntime;6 }7 privateRuntime() {}8 }

View Code

以上代码为JDK中Runtime类的部分实现,是饿汉式单例模式。在该类第一次被classloader加载的时候,实例就被创建出来了。一般不能实例化一个Runtime对象,应用程序也不能创建自己的 Runtime 类实例,但可以通过 getRuntime 方法获取当前Runtime运行时对象的引用。

验证:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 Runtime r1 =Runtime.getRuntime();2 Runtime r2 =Runtime.getRuntime();3 System.out.println(r1 == r2);

View Code

运行结果:

true

2、java.awt.Toolkit#getDefaultToolkit()

懒汉式单例。不需要事先创建好,只要在第一次真正用到的时候再创建就可以了。因为很多时候并不常用Java的GUI和其中的对象。如果使用饿汉单例的话会影响JVM的启动速度。

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 public abstract classToolkit {2

3 private staticToolkit toolkit;4

5 public static synchronizedToolkit getDefaultToolkit() {6 if (toolkit == null) {7 java.security.AccessController.doPrivileged(8 new java.security.PrivilegedAction() {9 publicVoid 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 (finalClassNotFoundException 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 = newHeadlessToolkit(toolkit);29 }30 }31 } catch (finalInstantiationException ignored) {32 throw new AWTError("Could not instantiate Toolkit: " +nm);33 } catch (finalIllegalAccessException ignored) {34 throw new AWTError("Could not access Toolkit: " +nm);35 }36 return null;37 }38 });39 loadAssistiveTechnologies();40 }41 returntoolkit;42 }43 }

View Code

以上代码是Toolkit类的单例实现。这里类加载时只静态声明了私有toolkit并没有创建Toolkit实例对象,延迟加载加快了JVM启动速度。单例模式作为一种创建模式,在依赖加载的时候应用了另一种创建对象的方式,不是new新的对象,因为Toolkit本身是个抽象类不能实例化对象,而是通过反射机制加载类并创建新的实例。

3、java.awt.GraphicsEnvironment#getLocalGraphicsEnvironment()

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 public abstract classGraphicsEnvironment {2 private staticGraphicsEnvironment localEnv;3 public static synchronizedGraphicsEnvironment getLocalGraphicsEnvironment() {4 if (localEnv == null) {5 localEnv =createGE();6 }7 returnlocalEnv;8 }9

10 private staticGraphicsEnvironment createGE() {11 GraphicsEnvironment ge;12 String nm = AccessController.doPrivileged(new GetPropertyAction("java.awt.graphicsenv", null));13 try{14 ClassgeCls;15 try{16 geCls = (Class)Class.forName(nm);17 } catch(ClassNotFoundException ex) {18 ClassLoader cl =ClassLoader.getSystemClassLoader();19 geCls = (Class)Class.forName(nm, true, cl);20 }21 ge =geCls.newInstance();22 if(isHeadless()) {23 ge = newHeadlessGraphicsEnvironment(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 returnge;35 }36 }

View Code

这里类加载时只静态声明了私有localEnv并没有创建实例对象。在GraphicsEnvironment类被第一次调用时会创建该对象。这里的createGE()方法也是通过反射的方式创建对象的。

总结:

(1)当一个类的对象只需要或者只可能有一个时,应该考虑单例模式。

(2)如果一个类的实例应该在JVM初始化时被创建出来,应该考虑使用饿汉式。

(3)如果一个类的实例不需要预先被创建,也许这个类的实例并不一定能用得上,也许这个类的实例创建过程比较耗费时间,也许就是真的没必要提前创建。那么应该考虑懒汉式。

(4)在使用懒汉式单例的时候,应该考虑到线程的安全性问题。


版权声明:本文为weixin_33970380原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。