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 9201743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:05:26+00:00 2026-06-17T23:05:26+00:00

In few articles, it says that using of volatile fixes the Double Checked Locking

  • 0

In few articles, it says that using of volatile fixes the Double Checked Locking issue.

class Foo {
        private volatile Helper helper = null;
        public Helper getHelper() {
            if (helper == null) {
                synchronized(this) {
                    if (helper == null)
                        helper = new Helper(); //Important
                }
            }
            return helper;
        }
    }

But here even if we use volatile for helper field, how can this be a safe publication ?
I mean how could this guarantee that we won’t get an in-consistence Helper object?

  • 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-06-17T23:05:28+00:00Added an answer on June 17, 2026 at 11:05 pm

    Without volatile, because the initial if (helper == null) is outside of a synchronized block, there is no visibility / consistency guarantee. In particular, it would be possible that helper would be not null but referred to an object that is only partially created.

    That is because helper = new Helper() is not an atomic operation:

    1. it allocates some memory
    2. it constructs the object
    3. it assigns a reference to the object to helper

    Without synchronization, an observing thread could see these operations in any order, in particular it could see 3 before 2.

    By making helper volatile, you introduces a happens-before relationship (as defined by the Java Memory Model) between the write and the read, which ensures that if you see 3 from an observing thread, you will also see 1 and 2.

    In particular, any operations executed before the volatile write will be visible from the volatile read (if it is subsequent of course).

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

Sidebar

Related Questions

I've read a few articles that sound similar, but I wasn't quite able to
I've seen a few articles, forum posts talking about uploading a file using NSURLRequest
I've been reading through quite a few articles on the 'this' keyword when using
I came across a few articles like this one , which suggest that some
I have found a few articles addressing the issue of helpers within an engine
I have read a few articles that BlueStacks App Player can be used to
I have read a few articles about .Net Entity Framework that really didn't make
I have read a few articles that say in order to have a PCI
I have read few articles about table partioning but still I am bit confused
I read few articles on jsplumb , but cannot yet understand the usage. I

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.