In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "java singleton pattern instance analysis". In daily operation, I believe many people have doubts about java singleton pattern instance analysis. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "java singleton pattern instance analysis". Next, please follow the editor to study!
In early project code, if we wanted to use a method of a class, we would basically create an object instance of a class and then call the method, and such an implementation would often have a large number of instances of a class in the system. In this way, the project framework is difficult to manage a large number of objects, and if the java virtual machine can not be recycled in time, it is easy to cause memory overflow.
First of all, we have to understand what a singleton is. The so-called singleton means that there is only one object instance of a class within the framework of the project, and any caller gets the same object instance. Then it is obvious that this class cannot be created by an external direct call to the class constructor.
Let's first look at a simple singleton design:
The above code is fine in a single thread, and the static variable of the class is generated only when the thread calls the static method of the class. However, when multi-threaded access, the above code is problematic, will generate multiple instances of objects.
Then, we can implement it in another way, such as initializing an instance of the object when the class is loaded, so that no new instance is created no matter how much the class static method is called later. There is another way, but at the expense of part of the system performance, which means that threads are queued to access the method through a lock mechanism when the method is accessed by multiple threads. Let's first implement the singleton of the class by putting a lock on the class method, such as:
The above method can achieve a single example, and the idea is delayed loading, but the execution efficiency is relatively low.
Previously, we have seen that some students use the double lock (Double CheckLock) mechanism to implement the singleton pattern. On the one hand, they need to add the volatile keyword to the instance to inform the operating system to implement the memory barrier during thread access, and then they need to synchronize the method access through the synchronized implemented by the virtual machine in the method, as follows:
Anyway, I think the above implementation is more complex, we need to understand more knowledge points, such as volatile, synchronized, memory barrier. Therefore, I do not recommend that you use this way, can be used as a technical understanding is still good, after all, if you can discuss this step with the interviewer, there will be some points.
If we do not consider the problem of service load, we can preload the static instance of the class in a multithreaded environment. When the virtual machine loads the class, it will create the static variable of the class. No matter whether you use it or not, I will leave it there for you. All threads access the same static instance. Some people also call this method "hungry Chinese". It is written as follows:
There is no problem with writing the implementation singleton above, but some students will think that if I just want to call a static method of a class and do not want to generate an instance of it, is there any other way? after the guidance of various gods combined with their own summary, we can use internal static classes to achieve this requirement.
As developers know, the virtual machine does not initialize the internal static class of the class at the beginning of the process of loading the class. If a thread calls an internal static class, the virtual machine is initialized only once, which is both singleton and thread-safe. It is written as follows:
In addition to the ways mentioned above, the enumeration type of JDK itself is the implementation of a singleton, and the caller cannot explicitly call the constructor to complete instance creation, so many Java specification documents recommend using enumerations to implement singletons.
Of course, for junior developers, the mainstream development frameworks now provide singleton / multi-instance patterns for developers to choose from, which makes developers pay more attention to business function development rather than the creation of virtual machine internal class instances. For example, default class injection in spring is singleton. You can set scope to singleton (singleton) or prototype (multiple instances) according to the actual situation, as shown in the following figure:
At this point, the study of "java singleton pattern instance analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.