single-case mode
One, single-threaded under the singleton mode
Singleton mode is only implemented on a single thread, and when it is not initialized at first, the TS is found to be null when concurrent access occurs, while several threads concurrently executing synchronously initialize a new TestSingle1 object.
Second, the simplest single case mode
The function thread is synchronized directly, ensuring that the function is not accessed during the first initialization, and the implementation of the single-instance mode in multi-threaded scenarios is ensured, but this function will lock and restrict access during the multi-threaded access, which actually affects the implementation efficiency of the Code. So it is not recommended to use this method.
a single model under static modification
Static modifiers are loaded statically, loaded once when the class is loaded, not repeatedly loaded, but will be loaded directly at the beginning of the object, with no lazy-loading effect. and to a certain extent, this is also a waste of memory resources.
Simple interest mode of static inner class implementation
the advantage of static inner classes is that they do not The load of the TestSingle4 class is loaded, but only when the getsingleinstance () function bar is loaded for the first time, but because it is static decorated, So after the first load is not repeated loading, so this method is to achieve the lazy-loading effect while ensuring thread safety. (recommended)
Five, theoretically realized two-time check single case mode
First, the synchronized modification is transferred to the inside of the function to ensure that the function lock is not performed in each invocation except for the first load;
Second, why double check? Because at the first load, if the concurrent thread accesses the getsingleinstance () function at the same time, it will enter the lock mode at the same time, if you do not make two judgments, locking is the first thread will be executed on the TS object is initialized, and then executes the thread, if not judged, will be re -initialization of the TS object, which violates the needs of our singleton mode;
Finally, why do we theoretically implement a singleton model?
Look directly at the code shown in six.
Six, in fact, the two-time verification of the single-case mode
StatementTsthe variable is preceded by an additionalvolatilekeyword, why do you need to add this? It is said that becauseJavaIn the process of compiling a virtual machine, the compilation sequence may be12-14-16it could be12-16-14, and once you encounter a12-16-14this way of compiling, because there is no advanced line -LineTSan empty judgment function jumps directly to the -the row returns an incorrect return value. And in the increase ofvolatilekeyword,JavaDuring the compilation process, the virtual machineTSparameters will be strictly logical, and will only appear12-14-16This form of compilation, so the program can execute normally.
not a good test method, but in the Java Singleton mode related to the introduction is indeed added volatile keyword.
Seven, unknown so the enumeration method of the single case mode
God's way of writing, first of all, only one enum object is declared in this class, which means that you want to invoke the method in this class, only by The INSTANCE object is called, and the parameters in the enumeration object are actually final and static decorated objects so there will only be one instance, and it will not be changed. the enum construction method Private does not have the means to initialize the object in other ways, which guarantees the thread's security.
The code is still a picture, and the hand knocks good health
Java design Pattern--Singleton mode