preface
Singleton mode is a creation mode. This class is responsible for creating its own objects and ensuring that only a single object is created. This class provides a way to access its unique object, which can be accessed directly without instantiating the object of this class.
1. Lazy style
public class SluggardSingleton { private static SluggardSingleton sluggardSingleton; private SluggardSingleton(){} public static SluggardSingleton getInstance(){ if (sluggardSingleton == null){ return new SluggardSingleton(); } return sluggardSingleton; } }
Lazy loading, when used, will check the example, return if there is one, and create a new one if there is none, so as to avoid memory waste. This method is non thread safe.
2. Lazy thread safety
public class SluggardSecuritySingleton { private static SluggardSecuritySingleton sluggardSecuritySingleton; private SluggardSecuritySingleton(){} public static synchronized SluggardSecuritySingleton getInstance(){ if (sluggardSecuritySingleton == null){ return new SluggardSecuritySingleton(); } return sluggardSecuritySingleton; } }
The only difference from the first method is that the synchronized keyword is added to ensure thread safety and affect the execution efficiency
3. Hungry Han style
public class HungrySingleton { private static HungrySingleton hungrySingleton = new HungrySingleton(); private HungrySingleton(){} public static HungrySingleton getInstance(){ return hungrySingleton; } }
Class is initialized when loaded, which wastes memory and ensures thread safety
4. Double check lock
public class DoubleCheckLockSingletion { private static DoubleCheckLockSingletion doubleCheckLockSingletion; private DoubleCheckLockSingletion(){} public static DoubleCheckLockSingletion getInstance(){ if (doubleCheckLockSingletion == null){ synchronized (DoubleCheckLockSingletion.class){ if (doubleCheckLockSingletion == null){ return new DoubleCheckLockSingletion(); } } } return doubleCheckLockSingletion; } }
The empty judgment processing of the outer instance does not require the thread to lock every time, but only when the instance is not created. At the same time, it can also ensure the safety of multithreading.
As for why to add another layer of air judgment in the inner layer, it is explained here by quoting the book "Dahua design mode":
If instance exists, it will be returned directly, which is no problem. When instance is null and two threads call GetInstance() method at the same time, they can all be judged by the first instance==null. Then, due to the lock mechanism, only one of the two threads enters and the other waits outside. One of them must enter and come out before the other can enter. At this time, without the judgment of whether the second instance is null or not, the first thread creates an instance, and the second thread can continue to create a new instance, which does not achieve the purpose of singleton.
5. Static internal class
public class StaticSingletion { private static class StaticSingletionHodel{ private static final StaticSingletion SINGLETION = new StaticSingletion(); } private StaticSingletion(){} public static StaticSingletion getInstance(){ return StaticSingletionHodel.SINGLETION; } }
This method can achieve the same effect as the double check lock method, but the implementation is simpler. This method is only applicable to the static domain. The double check lock method can be used when the instance domain needs to delay initialization.
It is different from the third method: the third method will be instantiated when the class is loaded, and this method will be instantiated only when called.
6. Enumeration
public enum EnumSingletion { INSTANCE; public void method() { } }
More concise, automatically supports serialization mechanism, absolutely prevents multiple instantiations, and ensures thread safety.
summary
Generally, lazy mode (thread safety and non thread safety) is not recommended, and the third hungry mode is recommended. The fifth method is only used when the lazy loading effect is to be clearly realized. If it involves deserialization to create objects, you can try to use the sixth enumeration method. If there are other special needs, the fourth double check lock mode can be considered.