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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:52:02+00:00 2026-05-21T08:52:02+00:00

In his talk about Effective Java at 54:15 Joshua Bloch recommends to use get

  • 0

In his talk about Effective Java at 54:15 Joshua Bloch recommends to use get before putIfAbsent in order to improve performance and concurrency. This leads me to the question why this optimization is already not build in like

public V BETTER_putIfAbsent(K key, V value) {
    V result = get(key);
    if (result!=null) return result;
    return ORIGINAL_putIfAbsent(key, value);
}
  • 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-21T08:52:02+00:00Added an answer on May 21, 2026 at 8:52 am

    This adds a double checked locking, the transactional semantics remains the same; so it is not wrong.

    Whether it is actually an optimization depends on usage. We are always tempted to check for special cases that we know cheaper solutions exist

    if A
        cheapSolutionForA();
    else
        genericSolution();
    

    This may work, or not – if A is rarely true, the additional check costs more than it saves. (and when A indeed is true on occasion, it can disrupt CPU branch prediction, it could have been cheaper to always go with the generic solution even when A=true)

    In Joshua’s example, A indeed is true frequently. He must be requesting intern string for the same string(same in value, not in identity) many times, therefore in most calls the map already has the key.

    If every call to intern() gets a different string, then the map never has the key, and his optimization backfires – the “optimization” costs more time, and saves none.

    Of course, when it comes to string intern, the 1st case is more realistic in practice .

    In general though, putIfAbsent() cannot predict how it’s being used, so it’s unwise to include this special case “optimization” inside it. In many use cases, contention is low, the map most likely doesn’t have the key when putIfAbsent is called, it would be wrong in these cases if the “optimzation” is built in.

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

Sidebar

Related Questions

Joshua Bloch in his Effective Java writes : Use the Javadoc @throws tag to
In his book Effective Java , Joshua Bloch recommends against using Interfaces to hold
Scott Gu didn't talk about this in his blog post. Will there be intellisense
In his PDC talk, Anders said that the dynamic keyword would dispatch any function
Use case: A does something on his box and gots stuck. He asks B
Meyers in his book 50 ways to improve... second edition writes that I must
How would one go about getting his or her code 'validated' to ensure it
I had a talk with my professor today about the layout of the Chunklist
In his blog post , Christian Cantrell shows how to use STORAGE_VOLUME_MOUNT in ActionScript.
On his blog Scott Kovatch writes: Without getting into too much detail, typing ‘java

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.