博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lintcode: Singleton && Summary: Synchronization and OOD
阅读量:5915 次
发布时间:2019-06-19

本文共 3491 字,大约阅读时间需要 11 分钟。

Singleton is a most widely used design pattern. If a class has and only has one instance at every moment, we call this design as singleton. For example, for class Mouse (not a animal mouse), we should design it in singleton.You job is to implement a getInstance method for given class, return the same instance of this class every time you call this method.ExampleIn Java:A a = A.getInstance();A b = A.getInstance();a should equal to b.ChallengeIf we call getInstance concurrently, can you make sure your code could run correctly?

单例模式,这是一道OOD的题

Eager initialization

This is a design pattern where an instance of a class is created much before it is actually required. Mostly it is done on system start up. In singleton pattern, it refers to create the singleton instance irrespective of whether any other class actually asked for its instance or not.

1 public class EagerSingleton { 2     private static volatile EagerSingleton instance = new EagerSingleton(); 3   4     // private constructor 5     private EagerSingleton() { 6     } 7   8     public static EagerSingleton getInstance() { 9         return instance;10     }11 }

Above method works fine, but has one drawback. Instance is created irrespective of it is required in runtime or not. If this instance is not big object and you can live with it being unused, this is best approach.

Lets solve above problem in next method.

Lazy initialization

In computer programming,  is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. In singleton pattern, it restricts the creation of instance until requested first time. Lets see in code:

1 public final class LazySingleton { 2     private static volatile LazySingleton instance = null; 3   4     // private constructor 5     private LazySingleton() { 6     } 7   8     public static LazySingleton getInstance() { 9         if (instance == null) {10             synchronized (LazySingleton.class) {11                 instance = new LazySingleton();12             }13         }14         return instance;15     }16 }

On first invocation, above method will check if instance is already created using instance variable. If there is no instance i.e. instance is null, it will create an instance and will return its reference. If instance is already created, it will simply return the reference of instance.

But, this method also has its own drawbacks. Lets see how. Suppose there are two threads T1 and T2. Both comes to create instance and execute “instance==null”, now both threads have identified instance variable to null thus assume they must create an instance. They sequentially goes to synchronized block and create the instances. At the end, we have two instances in our application.

This error can be solved using . This principle tells us to recheck the instance variable again in synchronized block in given below way:

Double-Checking Locking: (correct version)

1 public class EagerSingleton { 2     private static volatile EagerSingleton instance = null; 3   4     // private constructor 5     private EagerSingleton() { 6     } 7   8     public static EagerSingleton getInstance() { 9         if (instance == null) {10             synchronized (EagerSingleton.class) {11                 // Double check12                 if (instance == null) {13                     instance = new EagerSingleton();14                 }15             }16         }17         return instance;18     }19 }

 

转载地址:http://hrgpx.baihongyu.com/

你可能感兴趣的文章
Redhat/CentOS 6.x/7.x修改系统时区
查看>>
Lync 小技巧-41-Lync 2013-无法上载-PowerPoint
查看>>
7年Microsoft MVP,是否还能坚持3年
查看>>
CentOS下基于PPTPD与AD验证的×××服务器构建
查看>>
Java中的回调函数
查看>>
[每日一题] OCP1z0-047 :2013-07-21 子查询――多字段的顺序..............................................10...
查看>>
php加载模块
查看>>
在.net中读写config文件的各种方法
查看>>
如何正确对待和处理Windows更新问题?
查看>>
arcgis 拒绝访问 temp 目录
查看>>
[转]集中式日志系统 ELK 协议栈详解
查看>>
百度理解与交互技术 UNIT access_token获取接口
查看>>
linux重定向命令
查看>>
我的友情链接
查看>>
信号记录
查看>>
linux基础入门shell基础特性
查看>>
初步探索Linux(3)
查看>>
Linux下su与su -命令的本质区别
查看>>
硬盘与分区
查看>>
cacit下apache模版安装和使用
查看>>