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/03 Report--
This article will explain in detail how to implement the Java singleton. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The singleton pattern, as its name implies, means that only one instance is saved globally and can avoid manual instantiation by users, so the various writing methods of the singleton pattern have one thing in common, and objects cannot be created through the new keyword, so if you can instantiate it through the constructor, be sure to declare it private.
Public class PersonResource {public static final PersonResource PERSON_RESOURCE_SINGLETON = new PersonResource (); private PersonResource () {} public static PersonResource getInstance () {return PERSON_RESOURCE_SINGLETON;}}
This approach can be said to be the safest and simplest, but it has one drawback, that is, whether this instance is used or not, it will be instantiated, which is quite a waste of resources.
Lazy style one
Since the former method is a bit of a waste of resources, let the class be instantiated when it is called.
Public class PersonResource {private static PersonResource personResourceSingleton; private PersonResource () {} public static PersonResource getPersonResourceSingleton () {if (null==personResourceSingleton) {personResourceSingleton = new PersonResource ();} return personResourceSingleton;}}
This method can be initialized when the instance is needed, and it can also run well under single thread, but it is prone to problems if it is multithreaded.
Public class PersonResource {private static PersonResource personResourceSingleton; private PersonResource () {} public static PersonResource getPersonResourceSingleton () {if (null==personResourceSingleton) {personResourceSingleton = new PersonResource ();} return personResourceSingleton;}}
Multithreading has problems because multiple threads can execute getPersonResourceSingleton methods concurrently, causing problems when determining whether they are empty or not.
In that case, add a lock to make it mutually exclusive. There is another problem here. Every time you get an instance, you need to add a lock and unlock it, and when an instance has been generated, it is a bit redundant to add a lock.
Lazy triple (double check) public class PersonResource {private PersonResource () {} private volatile static PersonResource personResource; public static PersonResource getInstance () {if (personResource==null) {synchronized (PersonResource.class) {if (personResource==null) {personResource= new PersonResource ()} return personResource;}}
Since it is determined that the instance no longer needs to be locked after it is generated, we can solve the problem by judging whether an instance already exists before acquiring the lock.
Static inner class public class PersonResource {private PersonResource () {} private static class PersonResourceHolder {public static PersonResource personResourceSingleton = new PersonResource ();} public static PersonResource getInstance () {return PersonResourceHolder.personResourceSingleton;}}
In addition to the single exception that double checking can ensure security, it is also possible to hold a singleton with a static inner class, which ensures that it will not be loaded with the external class, which ensures delayed loading. at the same time, the singleton is instantiated when the class is loaded, which ensures thread safety.
Enumerate public enum PersonResource {/ * PersonResource singleton * / personResource; public void setPersonResource () {}} about "how to implement Java singleton" this article ends here, I hope the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
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.