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
  • 1 View
  • 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

Related Questions

Consider this code: val foo = if(true) new java.lang.Double(4) else new java.lang.Integer(4) The inferred
Is the double check lock pattern necessary when using ReaderWriterLockSlim ? Consider this scenario:
Consider this program int main() { float f = 11.22; double d = 44.55;
Consider this code: int main() { int e; prn(e); return 0; } void prn(double
Consider this program: #include <map> #include <vector> typedef std::vector<int> IntVector; typedef std::map<IntVector,double> Map; void
Consider a class like this one: class MyReferenceClass { public: MyReferenceClass(); const double ImportantConstant1;
Consider this code: double result = Math.Sqrt(4746073226998689451); For result I get 2178548422 instead of
Consider this simple code: int foo = 4; double d1 = sin (foo); double
A simple comparison of two double values in Java creates some problems. Let's consider
Consider MyClass.java: public class MyClass { public void firstfunction(double fwd[]) { fwd[0] = 42;

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.