Detailed example of Java singleton mode

Detailed example of Java singleton mode

Concept:

Singleton pattern is a common design pattern in Java. There are three kinds of singleton patterns: lazy singleton, hungry singleton and registered singleton.   

The singleton mode has the following features:

1. A singleton class can only have one instance. 2. A singleton class must create its own unique instance. 3. The singleton class must provide this instance to all other objects.   

The singleton pattern ensures that a class has only one instance, and instantiates itself and provides this instance to the whole system. In the computer system, the driver objects of thread pool, cache, log object, dialog box, printer and graphics card are often designed as singletons. These applications have more or less the function of resource manager. 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. The system should centrally manage these communication ports to avoid one communication port being called by two requests at the same time. In short, the singleton mode is selected to avoid inconsistency and political bullshit.

First look at a classic singleton implementation.

Singleton prevents the class from being instantiated externally by limiting the construction method to private. Within the scope of the same virtual machine, the only instance of singleton can only be accessed through the getInstance () method. (in fact, classes whose constructor is private can be instantiated through the java reflection mechanism, which will basically invalidate all Java singleton implementations. This problem is not discussed here. Let's hide our ears and think that the reflection mechanism does not exist.)

However, the above implementation does not consider thread safety. Thread safety means that if there are multiple threads running in the process of your code, these threads may run this code at the same time. If the result of each run is the same as that of a single thread, and the values of other variables are the same as expected, it is thread safe. In other words, the interface provided by a class or program is an atomic operation for threads or switching between multiple threads will not lead to ambiguity in the execution results of the interface, that is, we do not need to consider the problem of synchronization. Obviously, the above implementation does not meet the requirements of thread safety. Multiple singleton instances are likely to occur in a concurrent environment.

Operation results:

Conclusion: the results show that the singleton pattern provides a unique access point for an object-oriented application. No matter what function it implements, the whole application will share an instance object.

1. Hungry Chinese single instance

2. Lazy singleton

3. Registered single instance

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>