In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how the singleton pattern is used in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Be careful
1. A singleton class can only have one instance.
2. The singleton class must create its own unique instance.
3. The singleton class must provide this instance to all other objects.
Advantages
1. Having only one instance in memory reduces memory overhead, especially the frequent creation and destruction of instances (such as managing the academy home page cache).
two。 Avoid multiple occupations of resources (such as writing to files).
Shortcoming
1. There is no interface, can not be inherited, in conflict with the principle of single responsibility, a class should only care about internal logic, not how to instantiate outside.
Working with scen
1. A unique serial number is required for production.
The counters in 2.WEB do not have to be added to the database every time they are refreshed, but are cached first with a single example.
3. The creation of an object consumes too many resources, such as the connection between Imax O and the database.
First, the implementation method package com.asurplus.common.singleton.style1;import lombok.extern.slf4j.Slf4j;import java.util.Objects;@Slf4jpublic class ResUtils {private volatile static ResUtils instance = null / * Private constructor * / private ResUtils () {} / * provides the method to obtain instances * * @ return * / public static ResUtils getInstance () {/ / create if (Objects.isNull (instance)) {/ / avoid concurrent operations for empty talent Synchronized (ResUtils.class) {/ / create if (Objects.isNull (instance)) {/ / create a new object instance = new ResUtils () Log.info ("object created");} return instance;}}
We privatize its constructor so that an instance cannot be created externally, and we provide a way to get a unique instance so that we can get it externally.
2. Implementation method package com.asurplus.common.singleton.style2;import lombok.extern.slf4j.Slf4j;@Slf4jpublic class ResUtils2 {/ * static inner class * / private static class ResUtils2Holder {private static ResUtils2 instance = new ResUtils2 ();} / * provide the method to obtain instances * * @ return * / public static ResUtils2 getInstance () {return ResUtils2Holder.instance;}}
We use the static inner class method to create an instance, because the principle that JVM will only load once will eventually create only one instance and provide a way to get the instance so that we can get it from the outside.
3. Test package com.asurplus.common.singleton;import com.asurplus.common.singleton.style1.ResUtils;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * singleton mode * / public class TestMain {public static void main (String [] args) {/ / create thread pool ExecutorService executorService = Executors.newFixedThreadPool (10); for (int I = 0; I < 100) ExecutorService.shutdown +) {executorService.execute (ResUtils::getInstance);} executorService.shutdown ();}}
Output result
As you can see, we got 100 instances and created only one instance, thus implementing our singleton pattern.
Thank you for reading! This is the end of the article on "how to use the singleton pattern in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it 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.