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

Example Analysis of double-checked locking and Singleton pattern

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

Share

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

This article shows you the example analysis of double-checked locking and singleton pattern, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The singleton pattern ensures that there is only one instance of a class and instantiates itself and provides that instance to the entire system. In computer systems, driver objects for thread pools, caches, log objects, dialogs, printers and graphics cards are often designed as singletons.

The singleton pattern can be implemented as follows:

This is called deferred initialization, but it fails in the case of multithreading, so use a synchronization lock to lock the getInstance () method:

Synchronization requires overhead. We only need to synchronize during initialization, while the normal code execution path does not need synchronization, so there is a double check lock (DCL):

Such a design can guarantee that only one instance will be generated and that synchronization locks will only be added during initialization, which may seem exquisite, but it will lead to another problem caused by instruction reordering.

Instruction reordering is to optimize instructions and improve the efficiency of the program. Instruction reordering includes compiler reordering and runtime reordering. The JVM specification stipulates that instruction reordering can be carried out without affecting the execution results of single-threaded programs. For example, instance = new Singleton () can be decomposed into the following pseudocode:

But after reordering, it is as follows:

Changing the order of steps 2 and 3 does not affect the result of program execution in the single-threaded case, but it is different in the multithreaded case. Thread An executes instance = memory (which is visible to another thread B), and thread B executes the outer if (instance = = null), finds that the instance is not empty, and returns, but gets an instance that has not been fully initialized, which is bound to be risky when used, which is the problem with double-checked locking!

In view of the shortcomings of DCL, there is a revised version:

The revised version attempts to introduce local variables and a second synchronized to solve the problem of instruction reordering. However, although the Java language specification stipulates that the code within the synchronous code block must be executed before the object lock is released, it does not stipulate that the code outside the synchronous code block cannot be executed before the object lock is released, that is, instance = temp may be moved to the inner synchronized at compile time or run time, thus causing the same problems as DCL.

After JDK1.5, you can use the volatile variable to disable instruction reordering for DCL to take effect:

Another semantics of volatile is to ensure the visibility of variable modifications.

The singleton pattern can also be implemented as follows:

This is called delayed initialization placeholder (Holder) class mode. This mode introduces a static inner class (placeholder class), which initializes the instance in advance, which ensures not only the delayed initialization of Singleton instance, but also the synchronization. This is a comprehensive mode of early initialization (evil Chinese) and delayed initialization (lazy).

So far, the correct singleton pattern can be implemented in three ways:

1. Initialize ahead of time.

two。 Double check lock + volatile.

3. Delayed initialization of placeholder mode.

The above is the example analysis of double-checked locking and singleton pattern. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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

Internet Technology

Wechat

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

12
Report