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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:13:38+00:00 2026-05-10T22:13:38+00:00

Consider the double-check idiom for lazy initialization of instance fields: // Item 71 in

  • 0

Consider the ‘double-check idiom for lazy initialization of instance fields’:

// Item 71 in Effective Java copied from this interview with Bloch. private volatile FieldType field; FieldType getField() {     FieldType result = field;     if (result == null) { // First check (no locking)         synchronized(this) {             result = field;             if (result == null) // Second check (with locking)                 field = result = computeFieldValue();         }     }      return result; }

I want to be able to reset the field in a safe way (force it to load again from the database, in my case). I assume that we could do this by having a reset method:

 void reset() {    field = null; }

Is this the standard way of doing resetting the field? Is it safe? Any pitfalls? I’m asking because Bloch gave the following warning about double-checked lazy-loading: ‘The idiom is very fast but also complicated and delicate, so don’t be tempted to modify it in any way. Just copy and paste — normally not a good idea, but appropriate here.’

Thanks in advance, Playa from the Himalayas.

  • 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. 2026-05-10T22:13:38+00:00Added an answer on May 10, 2026 at 10:13 pm

    Yes, this is thread safe.

    The synchronized block is to prevent multiple threads from unnecessarily calling computeFieldValue(). Since field is volatile, the accesses in reset and getField are all well-ordered.

    If the first check is non-null, getField is done; result is returned.

    Otherwise, a lock is acquired, excluding any other thread that might set the field to non-null, but permitting any thread to set field to null. If any thread does set field to null, nothing should have changed; that’s the condition that got the thread into the synchronized block. If another thread had already acquired the lock after the current thread’s check, and set the field to a non-null value, the second check will detect that.

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

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Things like '\n' (not '/n' by the way) are escape… May 11, 2026 at 3:51 pm
  • added an answer In your configuration.php file, the base path might be defined,… May 11, 2026 at 3:51 pm
  • added an answer No, I don't think you can do that with attributes.… May 11, 2026 at 3:51 pm

Related Questions

I am planning on putting up a web service, or some other service exposed
I've been investigating the effort needed in getting menu items displayed in bold face
Consider the following two alternatives: console.log("double"); console.log('single'); The former uses double quotes around the
Consider the following signature in C#: double Divide(int numerator, int denominator); Is there a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.