Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Java singleton mode

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to write the Java singleton mode". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write the Java singleton pattern.

This model is very interesting and relatively simple, but I still have to say that because it is so widely used, so popular, a single case means a single, single seed, then what is the only one? Your mind is the only one, what else can't be copied? Let's take an object that is more difficult to copy: the emperor.

In the history of China, there are few periods in which two emperors coexist, yes, but not many, so we think that the emperor is a singleton model. in this scene, there are emperors, ministers, and ministers have to go to court to see the emperor every day. The emperor who visits the shrine today should be the same as yesterday and the day before yesterday (don't think about the transition period, don't find fault with him), the minister kowtowed and looked up. Hey, it's the same emperor as yesterday, the singleton mode. For the absolute singleton mode, first look at the class diagram:

Then let's look at the realization of the program and first decide on an emperor:

Package com.cbf4life.singleton1;/*** the history of China is generally a dynasty and an emperor. If there are two emperors, it is necessary to compete for an emperor * / public class Emperor {private static Emperor emperor = null / / define where an emperor is put, and then give the emperor's name private Emperor () {/ / secular and moral constraints to you, in order not to allow you to have a second emperor} public static Emperor getInstance () {if (emperor = = null) {/ / if the emperor has not been defined, then set an emperor = new Emperor () } return emperor;} / / what is the name of the emperor? public static void emperorInfo () {System.out.println ("I am the emperor.");}}

Then define the minister:

Minister package com.cbf4life.singleton1;/*** has to meet the emperor every day. The emperor he saw today is different from that of yesterday. That would be a problem. * / @ SuppressWarnings ("all") public class Minister {public static void main (String [] args) {/ / the first day Emperor emperor1=Emperor.getInstance (); emperor1.emperorInfo (); / / what was the name of the emperor who met on the first day? / / the next day Emperor emperor2=Emperor.getInstance (); Emperor.emperorInfo (); / / the third day Emperor emperor3=Emperor.getInstance (); emperor2.emperorInfo (); / / the emperors of the three days are all the same person, honor! }}

See, ministers see the same emperor every day, and there is no confusion. Anyway, they are all emperors, good or bad. As long as the emperor is mentioned, everyone knows who he is referring to. It is clear and clear. The problem is that this is the usual situation, and there is an example, for example, what should we do if there are two emperors in the same dynasty in one period?

Singleton model is very simple, that is, add a constructor in the constructor, access is private on it, this model is simple, but simple with risk, risk? What risk? In a J2EE S project, a thread is created after each HTTP Request request is sent to the container of BPUX, and each thread creates the same singleton object. What should we do? OK, let's write a general singleton program and analyze it:

Package com.cbf4life.singleton3;/*** generic singleton pattern * / @ SuppressWarnings ("all") public class SingletonPattern {private static SingletonPattern singletonPattern= null / / restrict that an instance private SingletonPattern () {} public SingletonPattern getInstance () {if (this.singletonPattern = = null) {/ / create a this.singletonPattern = new SingletonPattern ();} return this.singletonPattern;}} if there is no such instance.

Let's take a look at the yellow part. If there are two threads An and B now, thread An executes to this.singletonPattern = new SingletonPattern () and is applying for memory allocation. It may take 0.001 microseconds. Within these 0.001 microseconds, thread B executes to if (this.singletonPattern = = null). Do you think this judgment condition is true or false? It's true, and then what? Thread B also goes down, so there are two instances of SingletonPattern in memory to see if there is a problem.

What happens if your singleton is to get a serial number or create a signal resource? Business logic is confused! Data consistency check failed! The most important thing is that you can't see anything wrong with the code, which is the most fatal thing! Because basically you can't reproduce this kind of situation. Shudder, how do you modify it? There are many kinds of solutions, and I will say one that can solve the problem simply and thoroughly:

The package com.cbf4life.singleton3;/*** generic singleton pattern * / @ SuppressWarnings ("all") public class SingletonPattern {private static final SingletonPattern singletonPattern= new SingletonPattern (); / / restricts the ability to directly generate an instance private SingletonPattern () {} public synchronized static SingletonPattern getInstance () {return singletonPattern;}}

New an object directly to the member variable singletonpattern of the class, and getInstance () returns it directly to you when you want it, solving the problem!

At this point, I believe you have a deeper understanding of "how to write the Java singleton mode". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report