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

An example Analysis of java singleton Model

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

Share

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

This article introduces the relevant knowledge of "java singleton pattern example Analysis". 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!

Concept

Singleton pattern is an object instance in which only one class exists in JVM memory

classification

1. Lazy style

Create an instance when the class is loaded

2. Hungry Han style

Create an instance only when you use it

Of course, there are other ways to generate singletons, double check locks, enumerations and static inner classes, which will be introduced in this article.

Practice

Lazy style

1) Thread is unsafe and unavailable

Public class Singleton {

Private static Singleton instance

Private Singleton () {}

Public static Singleton getInstance () {

If (instance = = null) {

Instance = new Singleton ()

}

Return instance

}

}

2) Thread safety, synchronization method, low efficiency, not recommended

Public class Singleton {

Private static Singleton instance

Private Singleton () {}

Public static synchronized Singleton getInstance () {

If (instance = = null) {

Instance = new Singleton ()

}

Return instance

}

}

3) Thread is not safe, multiple instances will be generated and will not be available

Public class Singleton {

Private static Singleton singleton

Private Singleton () {}

Public static Singleton getInstance () {

If (singleton = = null) {

Synchronized (Singleton.class) {

Singleton = new Singleton ()

}

}

Return singleton

}

}

Han style, wireless range safety problems, can not delay loading, affecting the performance of the system

4)

Public class Singleton {

Private static Singleton instance = new Singleton ()

Private Singleton () {}

Public static Singleton getInstance () {

Return instance

}

}

5)

Public class Singleton {

Private static Singleton instance = null

Static {

Instance = new Singleton ()

}

Private Singleton () {}

Public static Singleton getInstance () {

Return instance

}

}

6) double check lock, thread safe, recommended

Public class Singleton {

Private static volatile Singleton singleton

Private Singleton () {}

Public static Singleton getInstance () {

If (singleton = = null) {

Synchronized (Singleton.class) {

If (singleton = = null) {

Singleton = new Singleton ()

}

}

}

Return singleton

}

}

7) static inner class, thread safe, instantiated only when active call, high efficiency of delayed loading, recommended

Public class Singleton {

Private static class SingletonHolder {

Private static final Singleton INSTANCE = new Singleton ()

}

Private Singleton () {}

Public static final Singleton getInstance () {

Return SingletonHolder.INSTANCE

}

}

8) enumerate types, wireless security issues, avoid anti-sequence Hua to create new instances, rarely use

Public enum Singleton {

INSTANCE

Public void whateverMethod () {

}

}

Matters needing attention

1. Consider the problem of multithreading

2. The singleton class constructor should be set to private type to prohibit the creation of external new.

Private Singleton () {}

3. If the class is serializable, consider deserialization to generate multiple instances. The solution is as follows

Private Object readResolve () throws ObjectStreamException {

/ / instead of the object we're on, return the class variable INSTANCE

Return INSTANCE

}

Working with scen

1. Tool object

2. There can be only one instance of a class in the system.

3. Create objects that are frequent or time-consuming, resource-consuming and frequently used

The following is the application of singleton pattern in JDK

In addition, the instance in the spring container is a singleton by default, that is, the bean is instantiated into the container when the container is started. Of course, you can also set lazy defalut-lazy-init= "true" to delay instantiation, and then instantiate when needed.

This is the end of the content of "java singleton pattern example Analysis". Thank you for your 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.

Share To

Internet Technology

Wechat

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

12
Report