Five writing methods of java design pattern's singleton pattern

Satiated mode (lazy mode)

 1 // Full Han
 2 // UnThreadSafe
 3 public class Singleton1 {
 4   private static Singleton1 singleton = null;
 5   private Singleton1() {
 6   }
 7   public static Singleton1 getInstance() {
 8     if (singleton == null) {
 9       singleton = new Singleton1();
10     }
11     return singleton;
12   }
13 }

Advantages: lazy load starts quickly, takes up little resources, instantiates only when using, no lock.

Disadvantage: non thread safe.

Fuller mode (lazy mode) - thread safety

 1 public class Singleton {
 2 
 3     /**
 4      * Define a variable to store the created class instance
 5      */
 6 
 7     private static Singleton uniqueInstance = null;
 8 
 9     /**
10      * Privatization construction method to control the number of instances created in internal control
11      */
12 
13     private Singleton(){
14     }
15 
16     /**
17      * Define a method to provide a class instance for the client
18      * @return An instance of Singleton
19      */
20 
21     public static synchronized Singleton getInstance(){
22 
23         //Determine whether the variables of the storage instance have values
24         if(uniqueInstance == null){
25             //If not, create a class instance and assign the value to the variable storing the class instance
26             uniqueInstance = new Singleton();
27         }
28 
29         //If it's worth it, use it directly
30         return uniqueInstance;
31     }
32 
33     /**
34      * Schematic method, a single example can have its own operation
35      */
36 
37     public void singletonOperation(){
38 
39 //Function processing
40 
41     }
42 
43     /**
44      * A single instance can have its own attributes
45      */
46 
47     private String singletonData;
48 
49     /**
50      * Sketch methods that allow external access to the value of the property
51      * @return Attribute value
52      */
53 
54     public String getSingletonData(){
55 
56         return singletonData;
57 
58     }
59 
60 }            

Advantages: same as above, but locked.

Disadvantages: synchronized is exclusive lock with poor concurrency performance. Even after the creation is successful, getting the instance is still a serialization operation.

Fuller mode (lazy mode) - Double Check Lock (DCL)

 1 public class Singleton {
 2 
 3     /**
 4      * Add the decoration of volatile to the variables of the saved instance
 5      */
 6 
 7     private volatile static Singleton instance = null;
 8 
 9     private Singleton(){
10 
11     }
12 
13     public static Singleton getInstance(){
14 
15 //First check whether the instance exists. If not, enter the following synchronization block
16 
17         if(instance == null){
18 
19 //Synchronization block, thread safe creation instance
20 
21             synchronized(Singleton.class){
22 
23 //Check whether the instance exists again. If not, create the instance
24 
25                 if(instance == null){
26 
27                     instance = new Singleton();
28 
29                 }
30 
31             }
32 
33         }
34 
35         return instance;
36 
37     }
38 
39 }

Advantages: lazy loading, thread safety.

Note: the instance must be decorated with the volatile keyword to ensure complete initialization.

Starving model

 1 public class Singleton {
 2 
 3 //4: define a static variable to store the created class instance
 4 
 5 //Create the class instance directly here, only once
 6 
 7     private static Singleton instance = new Singleton();
 8 
 9 //1: privatize the construction method to create the number of instances in internal control
10 
11     private Singleton(){
12 
13     }
14 
15 //2: define a method to provide a class instance for the client
16 
17 //3: this method needs to be defined as a class method, that is, static
18 
19 //There is no need to control the code in this method
20 
21     public static Singleton getInstance(){
22 
23 //5: directly use the created instance
24 
25         return instance;
26 
27     }
28 
29 }

Advantages: the starved mode is inherently thread safe and has no delay in use.

Disadvantage: instance is created at startup, which is slow to start and may cause resource waste.

Holder mode

 1 public class Singleton {
 2     /**
 3      * Class level inner class, that is, static member inner class, instances of the inner class and instances of the outer class
 4      * There is no binding relationship, and it will only be loaded when it is called, thus realizing delayed loading
 5      */
 6     private static class SingletonHolder{
 7         /**
 8          * Static initializer, JVM to ensure thread safety
 9          */
10         private static Singleton instance = new Singleton();
11     }
12     /**
13      * Privatization construction method
14      */
15     private Singleton(){
16     }
17     public static  Singleton getInstance(){
18         return SingletonHolder.instance;
19     }
20 }

Advantage: a perfect combination of lazy loading and thread safety (no lock). (recommended)

Remarks:

  1. Share the whole situation, only one;
  2. The constructor is not exposed (if exposed, there is no guarantee of one), and it is responsible for its own construction;
  3. Lazy type: Lazy load, load only when used, non thread safe. How to ensure thread safety:

(1) synchronized getInstance().

(2) double check and lock (volatile).

  1. Hungry Chinese style: apply at the beginning, waste a bit of resources, but its thread is safe.
  2. Holder mode:

(1) change to an internal class, and the JVM ensures thread safety.

Tags: Java jvm Attribute

Posted on Sun, 03 Nov 2019 06:55:35 -0800 by renegade888