Singleton mode is an object creation pattern that ensures that there is only one instance of a class in the system.
In the Java language, this has two major benefits:
1. For frequently used objects, you can omit the time to create the object;
2. As the number of new operations decreases, the frequency of system memory usage decreases, which reduces GC pressure and shortens GC pauses.
Single-Instance mode segmentation:
1.
1 Public classsingleton{2 PrivateSingleton () {3System.out.println ("Singleton.singleton ()");4 }5 6 Private StaticSingleton Singleton =NewSingleton ();7 8 Public StaticSingleton getinstance () {9 returnSingleton;Ten } One}
Note: First the Singleton class must have a private access level constructor to ensure that the singleton is not instantiated by the system's other code, and secondly, the singleton member variable and the getinstance () method must be static.
This singleton class is very simple to create and very reliable. The only drawback is that Singleton cannot be delayed loaded, for example, because the singleton creation process is slow, because the member variable is defined as static, when the JVM loads the singleton class, the Singleton object is created, and the singleton object is created wherever the Singleton class is used, regardless of whether the singleton object is used. For example:
Public classsingleton{PrivateSingleton () {System.out.println ("Singleton.singleton ()"); } Private StaticSingleton Singleton =NewSingleton (); Public StaticSingleton getinstance () {returnSingleton; } Public Static voidcreatestring () {System.out.println ("Singleton.createstring ()"); }}
2. In order to improve the call speed of related functions, lazy loading mechanism should be introduced.
1 PackageCom.luchao.singtonle;2 3 Public classLazysingleton {4 PrivateLazysingleton () {5System.out.println ("Lazysingleton.lazysingleton ()");6 }7 Private StaticLazysingleton lazyinstance =NULL;8 9 Public synchronized StaticLazysingleton getinstance () {Ten if(lazyinstance==NULL) OneLazyinstance =NewLazysingleton (); A returnlazyinstance; - } -}
For static variables, the 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 is already present, and if it exists, it is returned if it does not exist, and a singleton is built. GetInstance () must be a synchronous method, because in a multithreaded environment, when thread 1 is building a singleton, and before the assignment is completed, thread 2 may determine that instance is null, so thread 2 will start the process of creating a new singleton, resulting in multiple singleton creation.
The above example single implementation, although the implementation of delayed loading, but the introduction of synchronous method, in the multi-threaded environment, time-consuming is much larger than the first single-case program.
3. Singleton mode uses an internal class to maintain the creation of a singleton
1 Public classStaticsingleton {2 PrivateStaticsingleton () {3System.out.println ("Staticsingleton.staticsingleton ()");4 }5 6 Private Static classsingletonholder{7 Private StaticStaticsingleton Ataticsingleton =NewStaticsingleton ();8 } 9 Ten Public StaticStaticsingleton getinstance () { One returnSingletonholder.ataticsingleton; A } - -}
When Staticsingleton is loaded, the inner class is not instantiated, ensuring that the Singleton class is not initialized when the Staticsingleton class is loaded into the JVM, and when the getinstance () method is called, the Singletonholder is loaded, thereby initializing the instance. At the same time, the establishment of the instance is done at class loading, so it is inherently thread-friendly.
The use of internal classes to complete the simple interest mode, both for lazy loading, and do not use the Synchronization keyword, is a relatively perfect practice.
Java Design Optimization--single-instance mode