Consider an application that reads the contents of a configuration file. Many application projects have application-related profiles, many of which are customized by the project developer, and define some application-critical parameter data. Of course, in the actual project, this configuration file is mostly in XML format, but also in the properties format, we assume that we have created a class named AppConfig, which is specifically used to read the information in the configuration file. The client obtains an object that operates the contents of the configuration file by using the new instance of the AppConfig. If there are many places in the system running that need to use the contents of the configuration file, it is necessary to create an instance of the AppConfig object in many places. In other words, there are a lot of AppConfig instance objects in the system while the system is running, does the reader find any problems there? Of course, there is a problem, imagine that each AppConfig instance object inside the contents of the configuration file, there are multiple AppConfig instance objects in the system, which means that there will be multiple copies of the contents of the configuration file, which can seriously waste memory resources. The greater the content of the configuration file, the greater the amount of waste to system resources. In fact, for classes such as AppConfig, it is sufficient to require only one instance object during the run.
In specialization, a singleton pattern is an object creation pattern that is used to produce a concrete instance of an object that ensures that only one instance of a class is produced in the system. The singleton implemented in Java is the scope of a virtual machine, because the function of the load class is a virtual machine, so a virtual machine creates an instance of a class when it implements the Singleton class through its own classload load. In the Java language, this behavior can bring two major benefits:
For frequently used objects, you can omit the time it takes to create objects, which is a considerable overhead for those heavyweight objects;
As the number of new operations decreases, so does the frequency of system memory usage, which reduces GC pressure and reduces GC downtime.
Therefore, for the key components of the system and frequently used objects, the use of Singleton mode can effectively improve the performance of the system. The core of the singleton pattern is the return of a unique object instance through an interface. The first problem is to take the permission to create an instance and let the class itself take care of the creation of its own instance of the class, and then this class provides a way for external access to the class instance.
1, the basic realization of the Singleton mode
First, the Singleton class must have a private access level constructor to ensure that the singleton is not instantiated in other code in the system, and secondly, the instance member variable and the GetInstance method must be static.
Public class Singleton {
Private Static New Singleton (); Private Singleton () { } publicstatic Singleton getinsatnce () { return instance;} }
The only disadvantage of the above code is that the instance instance cannot be delayed loaded, for example, the creation of a singleton is slow, and since the instance member variable is static, the singleton object is created when the JVM loads the singleton class, and if this singleton class plays another role in the system , the singleton variable is initialized in any place where the singleton class is used, regardless of whether it will be used.
2. Delayed loading of a singleton mode
In order to solve this kind of problem, we need to introduce the delay loading mechanism
public class Singleton { private static Singleton Instance=null; private Singleton () {} public static synchronized Span style= "color: #000000;" > Singleton getinstance () { if (instance = null ) {instance = new Singleton (); return instance; }}
First, the static member variable instance initialization is assigned null to ensure that the system starts without additional load, and secondly, in the getinstance () factory method, determine whether the current singleton is already present, and if so, return if it exists, then establish a singleton. In particular, it is important to note that the getinstance () method must be synchronous, or in a multithreaded environment, when thread 1 is creating a new singleton, thread 2 May determine that instance is null before the assignment is completed, so thread 2 will also start a new singleton program, causing multiple instances to be created. Therefore, it is necessary to synchronize keywords. Because of the introduction of synchronization keywords, resulting in multi-threaded environment time-consuming increased significantly.
3
. Single-instance mode of
static inner class
Resolving the synchronization keyword to reduce system performance defects, you can use static internal classes, this method is also "effective Java" recommended.
Public classSingleton {PrivateSingleton () {}Private Static classSingletonholder {Private Static FinalSingleton INSTANCE =NewSingleton (); } Public Static FinalSingleton getinstance () {returnsingletonholder.instance; } }
When Staticsingleton is loaded, its inner class is not initialized, so it is ensured that when the Staticsingleton class is loaded into the JVM, the Singleton class is not initialized and the getinstance () method is called to load Singletonholder, thereby initializing the instance. At the same time, because the instance is built when the class is loaded, it is inherently multithreaded-friendly, and the getinstance () method does not need to use the Synchronization keyword.
The main content of this article comes from link https://www.ibm.com/developerworks/cn/java/j-lo-Singleton/
design mode (i): single-case mode