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 double check Lock by Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces Java how to achieve double check lock singleton related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this Java how to achieve double check lock singleton article will have a harvest, let's take a look.

Code implementation

The Java code is implemented as follows:

/ / double check lock singleton public class SingleInstance {/ / must be modified by volatile see Analysis 1 private volatile static SingleInstance instance / / privatization constructor private SingleInstance () {} public static SingleInstance getInstance () {/ / first void analysis 2 if (instance = = null) {synchronized (SingleInstance.class) {/ / second void analysis 3 if (instance = = null) {/ / Create a new instance instance = new SingleInstance () } return instance;}}

First of all, the synchronized keyword here does not modify the entire getInstance function, because this function may be used in many places, which will cause other threads to block, which is not very good, so only one piece of code is synchronized here.

Analysis 2: why to enter the synchronization code block need to be empty, if there are threads An and thread B, then thread A first determines that instance is null, so it enters the synchronization code block, creates an object, and then thread B comes in again, it does not have to enter the synchronization code fast, it can return directly, that is, lazy loading can speed up execution.

Analysis 3: why do you have to judge again in the synchronous code block? if there are threads An and B, the two A first call the method, and then B invokes the method, and then An and B are empty in analysis 2. So An enters the synchronous code block, B waits, when An enters the synchronous code block and creates the object, thread A releases the lock, and thread B enters again, if you don't analyze the 3 space at this time. B will create another instance, which is obviously against the rules.

Analysis 1: since you have added 2 layers of judgment, why add a volatile keyword? there are a lot of knowledge points here.

Because the code for creating a new instance:

Instance = new SingleInstance ()

It is not an atomic operation, and this simple assignment can be divided into three steps:

1. Allocate memory to SingleInstance

2. Call the construction method of SingleInstance

3. Point the instance to the allocated memory space

These are the three steps of normal logic, and the instance is not a null until you press 1 / 2 / 3.

But the Java memory model allows this to reorder instructions, that is, these three steps may be 123 or 132, so there is a problem here.

If thread An and thread B, thread A has run to the code at analysis 3, then the instruction execution is 132. just after the execution of step 3, thread B runs to the code at analysis 1 and will find that instance is not null, and thread B will return directly, resulting in an error.

Now that you know why, the volatile keyword solves this, which prohibits instruction reordering and ensures that all threads see that the variable is consistent, that is, it will not be read from the cache (we will talk about this feature later), so when creating an instance instance, its steps are 123, so it won't go wrong.

This is the end of the article on "how to achieve a double check lock in Java". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve double check lock in Java". If you want to learn more knowledge, you are welcome to follow 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

Development

Wechat

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

12
Report