In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to achieve the singleton model of java. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
Singleton pattern can be said to be one of the most commonly used design patterns, its main function is to ensure that there is only one instance of a class, and to provide a global access point to access it, strictly control the access mode of users.
The singleton model is divided into the lazy mode and the hungry mode. First of all, let's talk about the hungry mode.
Hungry man model
The hungry model is a bit hungry, just like a person who has been hungry for a long time, so as long as someone gives him something to eat, regardless of whether the food is delicious or not, whether he can eat it or not. The code is as follows:
PublicclassSingleton {
PrivatestaticSingletoninstance=newSingleton ()
PrivateSingleton () {}
PublicstaticSingletongetInstance () {
Returninstance
}
}
As we all know, it is a method of static initialization, as long as the object is instantiated as soon as the class is loaded, the advantage is thread safety, and the disadvantage is that the system resources are occupied in advance. That's when the lazy pattern emerged:
(recommended video: java video tutorial)
Lazy man mode
In contrast to the hungry mode, the lazy model means that the meal is not eaten until it is delivered to the mouth, otherwise the hungry will not move the mouth. The code is as follows:
PublicclassSingleton {
PrivatestaticSingletoninstance
PrivateSingleton () {}
PublicstaticSingletongetInstance () {
If (instance==null) {
Instance=newSingleton ()
}
Returninstance
}
}
However, when multiple threads call the getInstance () method at the same time, it is possible to create multiple instances, so this version of the thread is not safe, so there is the following version:
PublicclassSingleton {
PrivatestaticSingletoninstance
PrivateSingleton () {}
PublicstaticSingletongetInstance () {
If (instance==null) {
Synchronized (Singleton.class) {
If (instance==null) {
Instance=newSingleton ()
}
}
}
Returninstance
}
}
The method of double locking is used to ensure that when instance==null, when multiple threads call the getInstance () method, the problem can pass the first rejudgment.
In general, the singleton model of the hungry can meet most of the needs, and this is the basic situation of the singleton model.
These are all the contents of the article "how to achieve the singleton pattern of java". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.