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 realize singleton Mode by java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to achieve singleton mode in java". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve singleton mode in java".

Keystone

There can be only one instance of a class

Constructor privatization

It must create this instance on its own

Contains a static variable of this class to hold this unique instance

This example must be provided by the whole system first.

Provide a way to get the instance object

Direct exposure

Using the get method of static variables to get

Several common forms

Hungry Chinese style: there is no thread safety problem in creating objects directly.

Directly instantiate the hungry Han style (concise and intuitive)

Enumerated (most concise)

Static code block hungry Chinese style (suitable for complex instantiation)

Lazy style: delaying object creation

Thread unsafe (for single thread)

Thread safety (for multithreading)

Static internal column form (for multithreading)

Instantiate the object directly, regardless of whether it is needed or not-hungry Chinese style

/ * * hungry Chinese * (1) Constructor is privatized * (2) created by itself and saved with static variables * (3) provide this instance to the outside * (4) emphasize that this is a singleton, modified with final (this variable can only be assigned once and cannot be modified later) * / public class Singleton1 {public static final Singleton1 INSTANCE = new Singleton1 (); private Singleton1 () {}}

Enumeration-after jdk1.5-hungry Han style

/ * * hungry Chinese * enumerated type: indicates that the number of objects of this type is limited * We can limit it to one and it becomes a singleton * / public enum Singleton2 {INSTANCE}

Static code block-hungry Chinese style

/ * * this method is suitable for those who need to read a pile of information from the configuration file to instantiate. * / public class Singleton3 {public static final Singleton3 INSTANCE; private String info; static {try {Properties pro = new Properties (); / / read the information pro.load (Sinfleton3.class.getClassLoader (). GetResourceAsStream ("single.properties")) from the configuration file single.properties located in the src directory; INSTANCE = new Singleton3 (pro.getPropertied ("info")) } catch (IOExcepption e) {throw new RuntimeException (e);}} private Singleton3 (String info) {this.info = info;}}

4. Thread unsafe (suitable for single thread)-lazy style

/ * * lazy * (1) Constructor privatization * (2) static variable storage * (3) provides a static method to get the instance object * / public class Singleton4 {private static Singleton4 instance; private Singleton4 () {} public static Singleton4 getInstance () {if (instance = = null) {/ / plus Thread.sleep (100); two objects are instantiated in the case of multithreading. Instance = new Singleton4 ();} return instance;}}

Thread safety (suitable for multithreading)-lazy style

/ * * lazy * (1) Constructor privatized * (2) static variable storage * (3) provides a static method to obtain instance object * / public class Singleton5 {private static Singleton5 instance; private Singleton5 () {} public static Singleton5 getInstance () {if (instance = = null) {/ / determine whether it is empty or not, and then lock it. Synchronized (Singleton5.class) {/ / plus Thread.sleep (100); two objects are instantiated in the multithreaded case. Instance = new Singleton5 ();}} return instance;}}

Static inner class form

/ * the INSTANCE instance object is created only when the inner class is loaded and initialized * the static inner class is not automatically initialized with the loading and initialization of the external class, but is loaded and initialized separately * because it is created when the inner class is loaded and initialized, so it is thread-safe. * / public class Singleton6 {private Singleton6 () {} private static class Inner {private static final Singleton6 INSTANCE = new Singleton6 ();} public static Singleton6 getInstance () {return Inner.INSTANCE }} Thank you for your reading. the above is the content of "how to achieve the singleton pattern in java". After the study of this article, I believe you have a deeper understanding of how to realize the singleton pattern in java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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