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

What is the java double check lock mode

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

Share

Shulou(Shulou.com)05/31 Report--

This article "java double check lock mode is what" most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "java double check lock mode is what" article it.

cause

Encountered such a problem when conducting PMD static code testing on the project

Partially created objects can be returned by the Double Checked Locking pattern when used in Java. An optimizing JRE may assign a reference to the baz variable before it calls the constructor of the object the reference points to.

Note: With Java 5, you can make Double checked locking work, if you declare the variable to be volatile.

It roughly means that using the double-check lock mode, a partially initialized object may be returned. You may have some doubts about what is a partially initialized object. Let's continue to analyze it.

What is the double check lock mode public static Singleton getSingleton () {

If (instance = = null) {

Synchronized (Singleton.class) {

If (instance = = null) {

Instance = new Singleton ()

}

}

}

Return instance

}

We can see that instance = = null is judged both inside and outside the synchronization code block, because multiple threads may enter the if judgment outside the synchronization code block at the same time, and multiple instances may be initialized if null judgment is not made inside the synchronization code block.

The problem lies.

This way of writing seems perfect, but it is problematic, or it does not guarantee it will be perfect. The main reason is that instance = new Singleton (); is not an atomic operation.

Creating an object can be divided into three parts:

1. Allocate memory space for objects

two。 Initialize object

3. Set the instance to point to the memory address you just allocated

When instance points to an assigned address, instance is not null

However, between steps 2 and 3, it may be reordered, causing the object creation order to become 1-3-2. Imagine a scenario:

Thread A creates object Singleton for the first time, and the object creation order is 1-3-2.

When memory is allocated to instance, a thread B calls the getSingleton () method

At this time, the judgment of instance = = null is made, and it is found that instance is not null.

Note, however, that instance does not initialize the object at this time, and thread B will return the uninitialized object. Then thread B may have a problem when using instance, which is the problem with double-checked locks.

Use volatile

For the above problems, we can solve the problem by declaring instance as volly

Public class Singleton {

Private volatile static Singleton instance

Public static Singleton getSingleton () {

If (instance = = null) {

Synchronized (Singleton.class) {

If (instance = = null) {

Instance = new Singleton ()

}

}

}

Return instance

}

}

However, it must be used above the JDK5 version.

Static inner class public class Singleton {

Private static class SingletonHolder {

Private static final Singleton INSTANCE = new Singleton ()

}

Private Singleton () {}

Public static final Singleton getInstance () {

Return SingletonHolder.INSTANCE

}

}

This writing method is recommended at present, which adopts the way of static internal class, which not only realizes lazy loading but also does not have thread safety problems. And reduce the cost of synchronized.

The above is the content of this article on "what is the java double check lock mode". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

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