Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7098021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:00:19+00:00 2026-05-28T11:00:19+00:00

When I was reading Wikipedias’s article about Double Checked Locking idiom, I’m confused about

  • 0

When I was reading Wikipedias’s article about Double Checked Locking idiom, I’m confused about its implementation:

public class FinalWrapper<T> {
    public final T value;
    public FinalWrapper(T value) { 
        this.value = value; 
    }
} 
public class Foo {
    private FinalWrapper<Helper> helperWrapper = null;

    public Helper getHelper() {
        FinalWrapper<Helper> wrapper = helperWrapper;

        if (wrapper == null) {
            synchronized(this) {
                if (helperWrapper == null) {
                    helperWrapper = new FinalWrapper<Helper>(new Helper());
                }
                wrapper = helperWrapper;
            }
        }
        return wrapper.value;
    }
}

I simply don’t understand why we need to create wrapper. Isn’t this enough ?

if (helperWrapper == null) {
    synchronized(this) {
        if (helperWrapper == null) {
            helperWrapper = new FinalWrapper<Helper>(new Helper());
        }
    }
}    

Is it because using wrapper can speed up initialization because wrapper is stored on stack and helperWrapper is stored in heap?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:00:20+00:00Added an answer on May 28, 2026 at 11:00 am

    Isn’t this enough ?

    if (helperWrapper == null) {
         synchronized(this) {
           if (helperWrapper == null) {
              helperWrapper = new FinalWrapper<Helper>(new Helper());
           }
         }
    }
    

    No this isn’t enough.

    Above, first check for helperWrapper == null is not thread safe. It may return false (seeing non-null instance) for some thread “too early”, pointing to not fully constructed helperWrapper object.

    The very Wikipedia article you refer to, explains this issue step-by-step:

    For example, consider the following sequence of events:

    1. Thread A notices that the value is not initialized, so it obtains the lock and begins to initialize the value.
    2. Due to the semantics of some programming languages, the code generated by the compiler is allowed to update the shared variable to
      point to a partially constructed object before A has finished
      performing the initialization.
    3. Thread B notices that the shared variable has been initialized (or so it appears), and returns its value. Because thread B believes the
      value is already initialized, it does not acquire the lock. If B uses
      the object before all of the initialization done by A is seen by B
      (either because A has not finished initializing it or because some of
      the initialized values in the object have not yet percolated to the
      memory B uses (cache coherence)), the program will likely crash.

    Note semantics of some programming languages mentioned above is exactly the semantic of Java as of version 1.5 and higher. Java Memory Model (JSR-133) explicitly allows for such behavior – search the web for more details on that if you’re interested.

    Is it because using wrapper can speed up initialization because wrapper is stored on stack and helperWrapper is stored in heap?

    No, above isn’t the reason.

    The reason is thread safety. Again, semantic of Java 1.5 and higher (as defined in Java Memory Model) guarantees that any thread will be able to access only properly initialized Helper instance from wrapper due to the fact that it is a final field initialized in constructor – see JLS 17.5 Final Field Semantics.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading the wikipedia article on WPF architecture, I am a bit confused with
I was reading a Wikipedia article about Java EE application servers here: http://en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition#Java_EE_5_certified It
I'm reading about linked lists and their implementation in C on Wikipedia . I
While reading Wikipedia's page on decltype , I was curious about the statement, Its
Reading the Wikipedia article on UTF-8 , I've been wondering about the term overlong
I'm reading a Wikipedia article about OLAP and OLAP Fact Tables and the article
I'm reading the wikipedia about public-key Public-key cryptography ( http://en.wikipedia.org/wiki/Public-key_cryptography ) and in it
I was reading about App Engine on wikipedia and came across some GQL restrictions:
I've been reading about thread-safe singleton patterns here: http://en.wikipedia.org/wiki/Singleton_pattern#C.2B.2B_.28using_pthreads.29 And it says at the
I was reading about parsers and parser generators and found this statement in wikipedia's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.