In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "detailed explanation of java singleton mode". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Java singleton mode
A single case of hungry Han style
For the hungry pattern, we can understand that the singleton class is very hungry and urgently needs to eat, so it creates an object as soon as the class loads.
Lazy singleton class
For the lazy pattern, we can understand that the singleton class is very lazy, acts only when it is needed, and never knows to be prepared as soon as possible. It determines whether there is an object when it needs an object, creates an object immediately if not, and then returns it. If it no longer creates an object, it returns immediately.
Singleton design patterns are often used in JDBC linked databases.
Note:
We often use the first hungry Han style, because:
(1) since the singleton design pattern is adopted, it is to use the object of the singleton class, so instantiate it first.
(2) there are certain security risks in the lazy style, and the synchronization keyword synchronized needs to be added, otherwise it will not be a single case, but the efficiency will be slightly lower with the addition of synchronized
2 in the first approach, the code private static final Single SINGLE=new Single (); why is there final?
Because classes modified by the final keyword cannot be inherited, member variables modified by final cannot be modified.
Final is used here for reinforcement and emphasis: the concept of "one object"-there is only one object, and it cannot be modified.
If you do not use final to modify Single SINGLE, then there will be a situation where the business is very complex and the object is inadvertently modified, resulting in all kinds of errors.
The first kind (hungry Han style):
Train of thought:
(1) do not allow other classes to create objects of this class. Set the constructor to private!
(2) customize an object of this class
(3) provide custom objects. That is, you define a get method that returns an object whose value is this class.
Analysis:
Step 1: privatize the constructor in the singleton class and use a get () function to provide such objects, so other classes cannot construct such objects.
Step 2: but if other classes want to call the methods in this singleton class, they have to use the inner name. Method name (), which requires that the method must be static static.
Step 3: because static methods can only access static members! So set SINGLE to static
Public class SingleDemo {public static void main (String [] args) {Single s1=Single.getSingle (); s1.setNumber (44); Single s2=Single.getSingle (); s2.setNumber (55); System.out.println ("s1.number=" + s1.number); System.out.println ("s2.number=" + s2.number) If (s1.equals (S2)) {System.out.println ("S1 and S2 are the same object, that is, s1==s2"); / / if condition holds}} class Single {int number; public int getNumber () {return number;} public void setNumber (int number) {this.number = number;} private Single () {}; / / first step private static final Single SINGLE=new Single () / / step 3 public static Single getSingle () {/ / step 1 and step 2 return SINGLE;}}
The second (lazy type):
Package cn.com; public class SingleDemo2 {public static void main (String [] args) {Single s1=Single.getSingle (); s1.setNumber (44); Single s2=Single.getSingle (); s2.setNumber (55); System.out.println ("s1.number=" + s1.number); System.out.println ("s2.number=" + s2.number) If (s1.equals (S2)) {System.out.println ("S1 and S2 are the same object: s1==s2"); / / if condition holds}} class Single {int number; public int getNumber () {return number;} public void setNumber (int number) {this.number = number;} private Single () {}; private static Single SINGLE=null Public static synchronized Single getSingle () {/ / multithreading plus synchronized is the key! This is the end of if (SINGLE==null) {SINGLE=new Single (); return SINGLE;} else {return SINGLE;}} "detailed explanation of java singleton pattern". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.