第6章 创建型模式(单件模式).ppt

第6章 创建型模式(单件模式).ppt

* 为什么巧克力溢出了 遇到麻烦了——多线程问题 * * * 处理多线程 示例 * * 改善多线程 如果getinstance()的性能对应用程序不是很关键,就什么都别做。 使用“急切”创建实例,而不用延迟实例化的做法。 用“双重检查加锁”,在getinstance()中减少使用同步。 * 2、“急切”创建实例 public class Singleton{ private static Singleton uniqueInstance = new Singleton(); private Singleton(){ } public static Singleton getInstance() { return uniqueInstance; } } * 3、双重检查加锁(JAVA) public class Singleton{ private volatile static Singleton uniqueInstance=null; private Singleton(){ } public static Singleton getInstance() { if (uniqueInstance == null) { synchronized (Singleton.class) { if (uniqueInstance == null)

文档评论(0)

1亿VIP精品文档

相关文档