This is the 14th day of my participation in the Gwen Challenge.More article challenges
Preface:
We used the singleton pattern to optimize the connection pool resource in Redis. We used the singleton pattern to optimize the connection pool resource in Redis. In fact, in our daily development there are a lot of singleton pattern use scenarios such as thread pool and a series of common and limited resources, singleton pattern is not available, let’s take a closer look at the Java design pattern of a relatively simple creation pattern [singleton pattern];
Introduction:
The Singleton Pattern is one of the simplest design patterns in Java. This type of design pattern is the creation pattern, which provides the best way to create objects.
This pattern involves a single class that is responsible for creating its own objects while ensuring that only a single object is created. This class provides a way to access its unique objects directly, without instantiating the objects of the class. In computer systems, thread pools, caches, log objects, dialogs, printers, and graphics card driver objects are often designed as singletons. All of these applications function more or less as resource managers. Each computer can have several printers, but only one Printer Spooler, to avoid two print jobs being output to the Printer at the same time. Each computer can have several communication ports, which should be centrally managed by the system to avoid one communication port being invoked by two requests at the same time. In short, the singleton pattern is chosen to avoid inconsistent states;
Singleton mode features:
- A singleton class can have only one instance.
- A singleton class must create its own unique instance.
- The singleton class must provide this instance to all other objects.
Combining the above guarantees that a class has only one instance and provides a global access point to access it.
Usage scenario: When you want to control the number of instances and save system resources. For example, producing a unique sequence number is required, and creating an object consumes too many resources, such as I/O connections to the database.
Code implementation:
Lazy: thread-safe
public class Singleton { private static Singleton instance; private Singleton (){} public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; }}Copy the code
This approach is very lazy and works well in multiple threads, but it is inefficient and does not require synchronization 99% of the time. The advantage is that it is initialized only on the first call, avoiding memory waste. Disadvantages: synchronized must be added to ensure singletons, but locking will affect efficiency. The performance of getInstance() is not critical to the application (this method is used infrequently)
Hungry: Thread safety
public class Singleton { private static Singleton instance = new Singleton(); private Singleton (){} public static Singleton getInstance() { return instance; }}Copy the code
This approach is common, but tends to generate garbage objects. Advantages: Without locking, the execution efficiency will be improved. Disadvantages: Class initialization on load, waste of memory. It avoids multithreading synchronization problems based on the ClassLoader mechanism. However, instance is instantiated when the class is loaded. Although there are many reasons for class loading, in singleton mode the getInstance method is mostly called, However, there is no other way (or static way) to load the class, so initializing instance is obviously not lazy.
Double check lock:
public class Singleton { private volatile static Singleton singleton; private Singleton (){} public static Singleton getSingleton() { if (singleton == null) { synchronized (Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; }}Copy the code
This method adopts double lock mechanism, which is safe and can maintain high performance in the case of multiple threads. The performance of getInstance() is critical to the application.
In general, it is not recommended to use the first lazy way, it is recommended to use the second hungry way, double check lock mode eat!
Ok! Today to share this end, I hope it can be helpful to you, there are wrong places I hope you can put forward, grow together;
Neat makes for great code, and there’s only so much detail