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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:14:26+00:00 2026-06-06T13:14:26+00:00

Josh Bloch mentions using a local variable in double check idiom for possible performance

  • 0

Josh Bloch mentions using a local variable in double check idiom for possible performance improvement (EJ, 2nd ed. p284) and says the example code runs about 25% faster on his machine compared to that without a local variable. So the question is, why is it faster? In case of local variable version, it accesses the instance variable 3 times and in normal case it accesses the instance variable 4 times. Is this the reason behind ‘25%’ increase or are there any other reasons?

EDIT: 3 or 4 times access is only when the instance is first created. Thereafter, it’s 1 or 2 times.

EDIT2: check the accepted answer for this question to see the example code. I think this is for Java 6. How to solve the "Double-Checked Locking is Broken" Declaration in Java?

  • 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-06T13:14:28+00:00Added an answer on June 6, 2026 at 1:14 pm

    The basic thing is that accessing the volatile variable is slower, than accessing the local one. When you declare a local variable, you are basically caching the value of a volatile variable inside the method.

    In the normal (without a local variable) case you are accessing the volatile variable:

    1. In the first if before the synchronized clause
    2. In the second if inside the synchronized clause
    3. Inside the second if where you assign a value to it
    4. In the return statement

    Now, if you introduce a local variable, you only access the volatile variable three times:

    1. When assigning the local variable for the first if before the synchronized clause
    2. When assigning the local variable for the second if inside the synchronized clause
    3. Inside the second if where you assign a value to it

    You do not access it in the return statement, by returning the local variable, thus giving you a speed boost.

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

Sidebar

Related Questions

In the book Effective Java, Josh Bloch says that StringBuffer is largely obsolete and
I've been reading Josh Bloch's 'Effective Java 2nd Edition'. Item 43 states 'Return empty
In Josh Bloch's excellent book Effective Java under Item 39 he says: [D]efensive copies
I am reading Josh Bloch's book Effective Java and he suggests using a builder
I'm using the method described by Josh in this question to add a toolbar
I am learning MVVM using sample created by Josh Smith at http://msdn.microsoft.com/en-us/magazine/dd419663.aspx I wanted
At minute 4 of pry 's introductory screencast http://pry.github.com/ Josh Cheek says to run
Take a look at this java puzzles vid by Josh Bloch and William Pugh,
I have already read Josh Smiths article about binding commands to view model using
Question: I'm using Josh Smith's implementation of commands for MVVM using routed commands. 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.