Implementation of the Java Singleton Design Pattern

Definitions 1. singleton design pattern

Singleton design pattern ensures that only one instance of an object class, the class itself is responsible for creating your own objects and provide the example of the entire system. When accessing this object, visitors can get directly to the unique object without having to be instantiated by the visitor.

Singleton design pattern ensures the uniqueness of the global object, and have applications in many scenarios. Windows, for example, multiple processes or threads simultaneously operating a file, all the processes or threads must be done to deal with the same file through a unique instance.

Several singleton design pattern 2. java implementation

The most important feature is the singleton design class constructor is private , to ensure that the objects can only be created by the class itself, but visitors can not be instantiated. The following describes five kinds of commonly used java singleton design pattern implementations:

2.1 lazy style

. 1  public  class the Singleton {
 2      Private  static the Singleton instance;   // instance of a class 
. 3      Private the Singleton () {};
 . 4      
. 5    // If the instance has not been created, creating class instances and returns the acquired time 
. 6      public  static the Singleton the getInstance () {
 . 7          IF (instance == null ) {
 . 8              instance = new new the Singleton ();
 . 9          }
 10          return instance;
 . 11      }
 12 is }

Are Lazy initialization (ie, when the value of this field is required only to initialize): It is only created when the need to instantiate an object.

Are multi-thread safe : No

Lazy man is the most basic implementation, but not thread-safe, if multiple threads access the object classes, may have created a thread but because there is no synchronized, other threads can also create additional Singleton object. Therefore, this implementation can only work in single-threaded condition.

2.2 + synchronized synchronization lock lazy formula

 1 public class Singleton{
 2     private static Singleton instance;
 3     private Singleton(){};
 4     
 5     public static synchronized Singleton getInstance(){
 6         if(instance == null){
 7             instance = new Singleton();
 8         }
 9         return instance;
10     }
11 }

Are Lazy Initialization : Yes

Are multi-thread safe : Yes

In this way by giving getInstance () method plus synchronized synchronization lock , so that when a thread must wait for other objects in acquiring the object, other threads until after the current thread releases the lock to get the object, avoiding the problem of creating multiple objects. But this way because the other threads must wait, efficiency is very low.

2.3 starving formula

1 public class Singleton{
2     private static Singleton instance = new Singleton();
3     private Singleton(){};
4     
5     public static Singleton getInstance(){
6         return instance;
7     }
8 }

Are Lazy Initialization : No

Are multi-thread safe : Yes

Hungry Chinese-style class loader initialized when it created its own instance of an object, unless the system is restarted class reload, otherwise the class will always maintain that only one object, so thread-safe.

2.4 double checking locks

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

Are Lazy Initialization : Yes

Are multi-thread safe : Yes

Lock is a double check on the second optimization method above, since the entire synchronized getInstance () method to lock becomes less efficient, double check need only lock the lock portion of the lock, efficient execution.

Another point worth noting is that this implementation for instance variables using a volatile modifier modified, checked the two main roles:

  1. Ensure visibility. Use volatile variables defined will ensure that all threads are visible;
  2. Prohibit instruction reordering optimization.

More specifically understood that reference this blog .

2.5 static inner classes

 1 public class Singleton{
 2     private Singleton(){};
 3     
 4     private static class InnerSingleton{
 5         private static final Singleton INSTANCE = new Singleton();
 6     }
 7     public static final Singleton getInstance(){
 8         return InnerSingleton.INSTANCE;
 9     }
10 }

Are Lazy Initialization : Yes

Are multi-thread safe : Yes

Static inner classes use in the realization of the classloader mechanism to ensure that only one thread at initialization, and because INSTANCE adopted final modification, it can not be modified once created, to ensure the uniqueness of the object. Further, only display calls getInstance () method will be loaded when InnerSingleton class to instantiate the object.

 

Reference material

https://www.runoob.com/design-pattern/singleton-pattern.html

https://www.cnblogs.com/goodAndyxublog/p/11356402.html

 

Guess you like

Origin www.cnblogs.com/rezero/p/12568534.html