Singleton pattern is an object creation pattern that ensures that there is only one instance of a class in the system.
In the Java language, there are two benefits to doing this:
1. For frequently used objects, the time spent on creating objects can be omitted;
2. Due to the decrease in the number of new operations, the frequency of system memory usage is reduced, which reduces the pressure of GC and shortens the time for GC pause.
Singleton pattern segmentation:
1.
public class Singleton{ private Singleton(){ System.out.println("Singleton.Singleton()"); } private static Singleton singleton = new Singleton(); public static Singleton getInstance(){ return singleton; }}Note: First, the singleton class must have a private access level constructor to ensure that the singleton is not instantiated by other system code; second, the singleton member variable and getInstance() method must be static.
This singleton class is very simple to create and is very reliable. The only disadvantage is that singleton cannot be loaded latently. For example, because the singleton creation process is very slow and the member variable is defined as static, when jvm loads the singleton class, the singleton object will also be created. Then, a singleton object will be created anywhere a singleton class is used, regardless of whether the singleton object is used. For example:
public class Singleton{ private Singleton(){ System.out.println("Singleton.Singleton()"); } private static Singleton singleton = new Singleton(); public static Singleton getInstance(){ return singleton; } public static void createString(){ System.out.println("Singleton.createString()"); }} 2. In order to improve the calling speed of related functions, a lazy loading mechanism needs to be introduced.
package com.luchao.singtonle;public class LazySingleton { private LazySingleton() { System.out.println("LazySingleton.LazySingleton()"); } private static LazySingleton lazyInstance = null; public synchronized static LazySingleton getInstance(){ if(lazyInstance==null) lazyInstance = new LazySingleton(); return lazyInstance; }}The static variable singleton initialization assignment is null, ensuring that there is no additional load when the system starts. In the getInstance() method, it is determined that the current instance already exists. If it exists, it returns. If it does not exist, a singleton is created. getInstance() must be a synchronous method, because in a multi-threaded environment, when thread 1 is building a singleton and before the assignment is completed, thread 2 may determine that the instance is null, so thread 2 will start the program to create a new singleton, resulting in multiple singletons being created.
The above example singleton implementation implements delayed loading, but introduces a synchronization method, which takes much more time than the first singleton program in a multi-threaded environment.
3. Singleton pattern uses internal classes to maintain the creation of singletons
public class StaticSingleton { private StaticSingleton() { System.out.println("StaticSingleton.StaticSingleton()"); } private static class SingletonHolder{ private static StaticSingleton ataticSingleton = new StaticSingleton(); } public static StaticSingleton getInstance(){ return SingletonHolder.ataticSingleton; } }When StaticSingleton is loaded, the inner class will not be instantiated, ensuring that the singleton class will not be initialized when the StaticSingleton class is loaded into jvm, and the SingletonHolder is loaded when the getInstance() method is called, thereby initializing the instance. It is also used to create instances when the class is loaded, so it is naturally thread-friendly.
Using internal classes to complete the single-profit mode can not only achieve delayed loading, but also use synchronous keywords. It is a relatively complete approach.
The above is all the content of this article. I hope it will be helpful to everyone's learning.