Quick revision
The short answer
On Java 8, ConcurrentHashMap.computeIfAbsent locked the key's bin before it checked whether the key was already there, so every call on a hot key that already existed still took a lock and threads queued behind each other. The usual workaround was to call get first and fall back to computeIfAbsent only on a miss. Java 9 added a lock-free fast path that returns at once when the key is the first node of its bin, which fixes most cases; a key deeper in a crowded bin can still take the lock, so the get-first pattern can still help for very hot keys, and a profiler or JMH run on your actual JDK should decide.
What to remember
In this answer, we saw that Java 8's computeIfAbsent locked the bin before checking for the key, so hot keys that were already present caused lock contention. The standard workaround calls get first and falls back to computeIfAbsent only on a miss, which stays correct because the map still guarantees one computation per key. Java 9 added a lock-free fast path for a key at the head of its bin, which fixes most cases, but keys deeper in a crowded bin can still take the lock. The deciding step is a profiler or JMH run on the JDK we actually use.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
On Java 8, ConcurrentHashMap.computeIfAbsent locked the key's bin before it checked whether the key was already there, so every call on a hot key that already existed still took a lock and threads queued behind each other. The usual workaround was to call get first and fall back to computeIfAbsent only on a miss. Java 9 added a lock-free fast path that returns at once when the key is the first node of its bin, which fixes most cases; a key deeper in a crowded bin can still take the lock, so the get-first pattern can still help for very hot keys, and a profiler or JMH run on your actual JDK should decide.
Why this question is asked
computeIfAbsent became the standard way to build caches and registries in Java 8: one call, atomic, no race. Teams used it on the hottest paths of their services, often for keys that were present 99.9 percent of the time, and some of them then found threads parked inside ConcurrentHashMap in their profiles. Interviewers ask about it because it is a real performance bug in the JDK's own history, because the workaround is still seen in codebases today, and because the right answer depends on the JDK version. In this answer, we will recap how ConcurrentHashMap locks, read what Java 8 did on a hit, look at the workaround, see what Java 9 changed, and decide when the workaround still pays off.
1A Quick Recap of Bins and Locks
ConcurrentHashMap since Java 8 stores entries in an array of bins. A bin holds a short linked list of nodes, or a small tree when many keys collide. Reads with get never lock: they read the bin and walk its nodes using volatile reads, which make other threads' completed writes visible. Writes lock just one bin, using synchronized on the bin's first node, so updates to different bins run in parallel.
computeIfAbsent is a write-capable operation, since it may insert a value. The interesting question is what it does when the key is already present and nothing needs to be written. Ideally it should behave like get in that case: read, find the value, return it, no lock.
2What Java 8 Did on a Hit
Java 8's implementation did not have that shortcut. When the bin was not empty, it went straight into synchronized on the first node of the bin and only then walked the nodes looking for the key. So a call that found an existing value paid the same lock cost as a call that inserted one.
| JDK | Key is first node of bin | Key deeper in the bin |
|---|---|---|
| Java 8 | Bin lock taken | Bin lock taken |
| Java 9 and later | Returned without locking | Lock may still be taken |
get() then computeIfAbsent on miss | No lock on a hit | No lock on a hit |
For a key read by one thread at a time, an uncontended synchronized block is cheap. But think of a cache of 50 parser objects keyed by format name, where every request on 64 threads asks for "json". All 64 threads need the lock on the same bin for every request. They take turns, the lock inflates to a heavyweight monitor, and profilers show threads BLOCKED inside computeIfAbsent although the map never changes. This was reported against the JDK as a performance problem (JDK-8161372).
3The get-First Workaround
The fix that spread through codebases was simple: try the lock-free get first, and only call computeIfAbsent when the value is missing.
Notice why this is still correct. If two threads both miss, both call computeIfAbsent, and the map guarantees that only one of them runs f; the other waits briefly and receives the same value. The get only adds a fast exit; it never decides anything on its own. The cost is a second lookup on a miss, which is negligible when misses are rare. The one caveat is null: ConcurrentHashMap never stores null values, so a null from get always means absent, which keeps the pattern simple.
4What Java 9 Changed
Java 9 added a fast path at the top of computeIfAbsent. Before locking, it looks at the first node of the bin without a lock. If that node's hash matches and its key equals ours and it has a value, it returns that value straight away. Only if the first node is a different key, or the bin is being moved during a resize, does it continue to the locking path.
In a well-sized map, most bins hold zero or one node, so for most keys the first node is the key we want, and a hit now costs about the same as get. That removes the contention for the parser-cache example above. But there is a catch: when two or more keys share a bin, only the first one benefits. A hot key that sits second in its bin still goes through synchronized on every call. Tree bins, which appear when a bin collects eight or more nodes, also take the locking path for anything beyond the head.
5Does the Workaround Still Apply?
On Java 17, 21 or 25, the get-first pattern is rarely needed for ordinary code, and adding it everywhere makes the code noisier for no gain. It still helps in two situations: a very hot key that collides with another key in its bin, and maps with poor hash distribution where bins are long. It also costs almost nothing to keep in the few hottest paths, because a hit is still one lock-free lookup.
So the honest answer is to measure on the JDK we run. A JMH benchmark, the Java microbenchmark harness, with 32 threads calling computeIfAbsent on present keys shows whether there is any difference, and a profiler such as JFR or async-profiler in production shows whether threads are actually blocked on ConcurrentHashMap monitors. If the lock does not appear in the profile, the simpler single call is the better code.
6Follow-up questions
Why did Java 8 lock before checking in the first place?
The implementation treated computeIfAbsent as an update, and the simplest correct way to make check-and-insert atomic is to hold the bin lock for both. The lock-free check is safe only because a stale read can never produce a wrong answer here: if the key is present, returning its value is correct, and if the read misses, the locked path rechecks.
How would you recognize this in a thread dump?
Many threads in state BLOCKED with frames inside ConcurrentHashMap.computeIfAbsent, waiting to lock a ConcurrentHashMap$Node, while one thread holds that monitor. If the map is almost never updated and the JDK is 8, this pattern points straight at the missing fast path.
7Conclusion
In this answer, we saw that Java 8's computeIfAbsent locked the bin before checking for the key, so hot keys that were already present caused lock contention. The standard workaround calls get first and falls back to computeIfAbsent only on a miss, which stays correct because the map still guarantees one computation per key. Java 9 added a lock-free fast path for a key at the head of its bin, which fixes most cases, but keys deeper in a crowded bin can still take the lock. The deciding step is a profiler or JMH run on the JDK we actually use.
Open with
On Java 8, computeIfAbsent went straight into the synchronized block on the first node of the bin and only then looked for the key. So even when the key was already present and nothing needed to change, every call took the bin lock.
Then, if they want more
For a hot key used by many threads, like a cache of formatters keyed by name, all those threads serialized on one monitor, and you saw them BLOCKED inside computeIfAbsent in thread dumps. The usual workaround was to call get first, which never locks, and only call computeIfAbsent when get returns null.
That is still correct, because if two threads both miss, the map still runs the mapping function only once. Java 9 added a fast path: before locking, it checks the first node of the bin, and if that is our key with a value it returns immediately.
That fixes it for most keys, because in a well-sized map most bins have one node. But if the hot key is second in a bin because of a collision, or the bin is a tree, it can still take the lock, so the get-first pattern can still help for the very hottest keys.
On a modern JDK I would not add it everywhere; I would confirm with JFR or async-profiler that threads are actually blocked on the map, or with a JMH benchmark on our JDK, and keep the simple version otherwise.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8, Java 9, Java 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "On Java 8, profiling showed threads contending inside ConcurrentHashMap.computeIfAbsent...": https://scrivio-interview-library.netlify.app/java/concurrent-collections/1b248bef3f/