Recently saw Brian Goetz I wrote an article about DCL The article :Double-checked locking: Clever, but broken.(2001 Published in JavaWorld On )

This article tells about DCL The original intention of the design , But because JVM Different implementations of ( Not strictly following JMM standard ) Lead to DCL Failure in practical application .

1. DCL Design intention of

DCL To support Lazy initialization And designed .

There are many ways to implement the singleton pattern :

  • Eager initialization: Hungry loading , Class variables are initialized when the class loader loads the class . In this way, the atomicity of initialization can be guaranteed , But it reduces the startup performance of the program ( Class variables are initialized in the initialization phase of class loading ).
  • Lazy initialization: Lazy loading , Initialize when class variables are needed . This way can improve the startup performance of the program , But synchronization is needed to ensure atomicity of initialization , There's no doubt synchronization overhead .

DCL The goal is (1) Improve the startup performance of the program ,(2) Reduce synchronization overhead .

Code example :

 class SomeClass {
private Resource resource = null;
public Resource getResource() {
if (resource == null)
resource = new Resource();
return resource;

2. DCL The cause of the failure

DCL To be really effective , Need to rely on JVM Really followed JMM standard .

In the actual , compiler 、 processor 、 Cache is OK “ free ” Execute code ( Disorderly execution ), Just promise as-if-serial semantics Just go .

Here I want to mention synchronized The role of :

  • Mutex executes a piece of code
  • Trigger memory barrier , Force the working memory of the thread to synchronize with the living memory

Proper use of synchronization ensures : One thread expects to see the impact of another (The proper synchronization guarantees that one thread will see the effects of another in a predictable manner).

DCL The reason for this failure , It's because it's not used synchronously resource.( Related to the disorderly execution mentioned above , This is related to the concrete JVM The version is about , quite a lot JVM Implementation is not strictly implemented JMM standard )

3. Solution

(1) Don't use DCL;

(2) Use load (Eager initialization), The class loader guarantees the mutual exclusion of class variable initialization ( Each class will only load once );

(3) There is a special case of lazy loading : Contains only one class variable , No class methods 、 Instance variables 、 Example method ;

(4) Yes 32bit primitive values It works .

DCL The failure of : More articles about the deviation of reality from original intention

  1. Design Patterns in Android

    Yes, everyday in Android In the practical design mode to sort out and summarize , This article refers to some websites and big man's blog , Such as MichaelX(xiong_it) . Novice tutorial . April grapes .IAM 42, etc , Note here ~ In addition, it is highly recommended that ...

  2. Android Classic design patterns

    [ The singleton pattern ][Build Pattern ][ Archetypal model ][ Factory mode ][ The strategy pattern ][ The state pattern ][ The chain of responsibility model ][ Interpreter mode ][ Command mode ][ Observer mode ][ Memo mode ][ Iterator pattern ] [ Template method pattern ][ Visitor mode ][ Intermediary ...

  3. Android Summary of the creative model in

    common 5 Kind of , The singleton pattern . Factory method model . Abstract factory pattern . Builder pattern . Archetypal model The singleton pattern Definition : Make sure there is only one instance of a class , And provide this instance to other classes . Use scenario of singleton mode : It takes a lot of resources to create a class ,new One ...

  4. python Advanced applications for

    Make a note of Python Functional programming , Advanced ones BIF, Usage and experience of advanced official library . Functional programming Functional programming uses a series of functions to solve problems , According to the general programming thinking , The way we think about problems is " How to do it &quo ...

  5. SQL SERVER in is null and is not null Will the index fail ?

    In fact, there was nothing to say about this issue , Today, I met a SQL sentence , Because it's more interesting , So I intercept . To simplify the SQL sentence , Let's show you , As shown below declare @bamboo_Code varchar(3); ...

  6. ddl dml dcl

    DCL Data control language Create a temporary table space create temporary tablespace user_temp tempfile 'E:/oracle/product/10.1.0/oradata/o ...

  7. use happen-before Review the rules DCL( turn )

    To write Java Multithreading has always been a very difficult thing , Multithreaded program bug It's hard to test ,DCL(Double Check Lock) It's a typical example , Therefore, the theoretical analysis of multithreading security is very important , Of course, this is by no means to say that to many lines ...


    DCL(Data Control Language) It's database control language . Is used to set or change the database user or role permissions statement , Include (grant,deny,revoke etc. ) sentence . By default , Only sysadmi ...

  9. DML,DDL,DCL,DQL The difference between

      DML English abbreviation DML = Data Manipulation Language, Data manipulation language , A computer language that enables users to query databases and manipulate data in existing databases . Specifically, it means UPDATE to update .INS ...

Random recommendation

  1. OO in , Object first or class first ?

    Just ask , In object-oriented thinking , Object first or class first , At first glance, it seems to be the same kind of problem as egg or chicken , It's not ! This problem , stay lz I was asked by the interviewer when I took the postgraduate entrance examination , As like as two peas , Now I see a similar topic in a written test , Blink , Someone will ...

  2. UI3_UICollectionViewMuti

    // AppDelegate.m // UI3_UICollectionViewMuti // // Created by zhangxueming on 15/7/16. // Copyright ...

  3. Various sorting algorithms and c Language implementation

    Insertion sort O(n^2) Bubble sort O(n^2) Selection sort O(n^2) Quick sort O(n log n) Heap sort O(n log n) Merge sort O(n log n) Shell Sort O(n^1.25) 1. insert ...

  4. POI operation Excel Summary of common methods

    Reprinted from : One . POI brief introduction Apache POI yes Apache Open source library of Software Foundation ,POI carry ...

  5. C# Comparison of common unit testing frameworks :XUnit, NUnit, and Visual Studio(MSTest)

    Students who have done unit testing probably know the above testing frameworks , But I've always wondered what's the difference between them , Then I found a good article that clearly explained the biggest difference between these frameworks . The address is here : ...

  6. linux Common settings of cloud server

    In front of the word Due to the configuration of your own website on the cloud server , take Linux The common settings in are recorded as follows change shell By default , ubuntu By default shell yes dash, But it's more common shell yes bash You can use the following command to da ...

  7. CodeForces - 556B Case of Fake Numbers

    //////////////////////////////////////////////////////////////////////////////////////////////////// ...

  8. Leetcode_165_Compare Version Numbers

    This article is a summary of my study , Welcome to reprint, but please indicate the source : Compare two version nu ...

  9. TCP / IP,HTTP

    When I was studying the basis of network in University, the teacher said , The network is divided into physical layer from bottom to top . Data link layer . The network layer . Transport layer . The session layer . Presentation layer and application layer . Through a preliminary understanding of , That's true. IP The protocol corresponds to the network layer ,TCP The protocol corresponds to the transport layer , and HTTP The protocol corresponds to the application ...

  10. 【codeforces 718E】E. Matvey's Birthday

    The main idea of the topic & link : Give a long for n(n<=100 000) Contains only ‘a’~‘h’8 Character string ...