Singleton (II)

 

 

Foreword

Singleton in peacetime as relatively common, there are a variety of writing the java singletons, where analysis of a relatively good writing.

Code

public class Test {
    private volatile static Test instance;
 
    private Test() {
 
    }
 
    public static Test getInstance() {
        if (instance == null) {
            synchronized (Test.class) {
                if (instance == null) {
                    instance = new Test();
                }
            }
        }
        return instance;
    }
}

Code explanation:

    • Note that the first point: using a private constructor, to ensure that under normal circumstances can not be outside the class initialized (non-initialized normal conditions, such as by reflection, generally after a reflection singleton loses the effect).
    • Note that the second points: getInstance determination method of the first empty condition, the logic can be removed, after removing not affect the correctness of a single embodiment, but after removal inefficient. Because then removed, regardless of whether the instance has been initialized, it will conduct synchronized operations, and synchronized operation is a heavy consumption performance. After plus, if you have initialized direct returns the result, it will not be synchronized operations.
    • The third point Note: plus synchronized to prevent concurrency issues when multiple threads simultaneously calling getInstance method, initialize each instance over again.
    • Note fourth points: getInstance second judgment method can not remove an empty condition, if removed, and exactly two threads a and b by the first determination condition empty. A first case it is assumed to acquire the lock, enter the synchronized code block, initialized instance, a release lock. Then b acquiring the lock, enter the synchronized code block, but also directly initialize instance, instance is initialized many times do not meet the requirements of singleton ~. After air plus a second judgment condition, b into the lock to obtain a synchronized block of code, this time instance is not empty, the initialization operation is not performed.
    • The fifth point Note: instance of a statement voliate key, if not the key, there may be an exception. Because instance = new Test (); not an atomic operation, it will be compiled into three instructions as shown below.
      1. The memory allocated to Test Examples 2 Test constructor initializes the instance objects 3. The pointer to the allocated memory space (note that this case is not empty instance)
      then blanket, java will reorder instructions, JVM characteristics according to the processor full use of multi-level cache, and so multi-core reordering appropriate instructions to cause the program to run the business while ensuring full use of the characteristics of execution of the CPU, the maximum performance of the machine to play! It is simply jvm implementation of the above three instructions when this is not necessarily the execution 1-2-3, 1-3-2 so there may be executed. If jvm is then performed according to 1-3-2, 1-3 when executed yet been performed 2, if another thread calls the getInstance (), because the case 3 is performed instance is not empty, return directly instance. 2 The problem is not implemented, this time instance is equivalent to nothing, there is definitely a problem. Then blanket, voliate has a feature that prohibits instruction reordering, the above three instructions are executed in accordance with 1-2-3, so there is no problem.

 

Guess you like

Origin www.cnblogs.com/JonaLin/p/11533218.html
ii