当前位置:首页 > 服务端 > Lintcode: Singleton && Summary: Synchronization and OOD

Lintcode: Singleton && Summary: Synchronization and OOD

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.

Example
In Java:

A a = A.getInstance();
A b = A.getInstance();
a should equal to b.

Challenge
If 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, lazy initialization 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 double-checked locking. 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 check
12                 if (instance == null) {
13                     instance = new EagerSingleton();
14                 }
15             }
16         }
17         return instance;
18     }
19 }

 

作者:neverlandly
来源链接:https://www.cnblogs.com/EdwardLiu/p/4443230.html

版权声明:
1、Java侠(https://www.javaxia.com)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaxia.com/server/124683.html

标签: unused variable
分享给朋友:

“Lintcode: Singleton && Summary: Synchronization and OOD” 的相关文章

pycharm安装教程,超详细2022年05月16日 21:28:02
软件工程复习要点2022年05月17日 14:38:01
Logger打印等级设置2022年05月19日 20:04:13
集成极光推送遇到的问题2022年05月21日 21:05:48
关于设计模式2022年05月23日 23:02:13
信息系统合同管理2022年05月24日 22:14:31