Quick revision
The short answer
ConcurrentHashMap keeps its entries in an array of bins that it reads with volatile semantics, and each node's value and next pointer are volatile, so get() takes no lock and always sees fully published nodes. put() into an empty bin installs the first node with a single CAS, and put() into a non-empty bin synchronizes only on that bin's first node, so writers to different bins never block each other. Crowded bins become TreeBins as in HashMap, and the size is kept in a base counter plus striped counter cells, similar to LongAdder, to avoid one contended counter.
What to remember
In this answer, we saw that ConcurrentHashMap avoids a global lock by combining three techniques: volatile reads of bins and of each node's value and next pointer, so get() never locks and always sees fully published nodes; a CAS to install the first node of an empty bin; and a synchronized block on the first node of a non-empty bin, so writers only block writers of the same bin. Crowded bins become TreeBins whose readers fall back to the linked list while a writer restructures, and the size lives in a base counter plus striped counter cells. Per-bin locking is what replaced the Java 7 segments.
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.
ConcurrentHashMap keeps its entries in an array of bins that it reads with volatile semantics, and each node's value and next pointer are volatile, so get() takes no lock and always sees fully published nodes. put() into an empty bin installs the first node with a single CAS, and put() into a non-empty bin synchronizes only on that bin's first node, so writers to different bins never block each other. Crowded bins become TreeBins as in HashMap, and the size is kept in a base counter plus striped counter cells, similar to LongAdder, to avoid one contended counter.
Why this question is asked
ConcurrentHashMap is the map we reach for whenever several threads share state: caches, registries, counters per key. It is fast because it avoids a global lock, and knowing how it does that explains its behaviour, from why it rejects null keys to why size() is only an estimate under load. Interviewers ask it to see whether we understand CAS, volatile visibility and fine-grained locking working together. In this answer, we will recap the table, follow get() and then put() in both of its cases, look at tree bins and the size counter, and answer the two follow-ups about what replaced segments and why get() can run during a write.
1A Quick Recap of the Table
Like HashMap, ConcurrentHashMap stores entries in an array called the table, and each slot is a bin holding a linked list of nodes whose keys hash to that slot. A node holds the hash, the key, the value and a pointer to the next node. Two details differ from HashMap and make concurrency possible. The table slots are read and written with volatile semantics, through special array access methods, so a thread always sees the latest node installed in a slot. And in each node, the value and the next pointer are volatile fields, so changes to them are visible to other threads immediately and in order.
Volatile here means the Java memory model's guarantee: a write to a volatile field happens-before any later read of it, so everything the writer did before the write, such as filling in a new node's key and value, is visible to the reader.
2How get() Works Without a Lock
get() computes the spread hash of the key, reads the bin at (n - 1) & hash with a volatile read, and checks the first node. If its hash and key match, it returns the volatile value. Otherwise it walks the next pointers. If the first node has a negative hash, it is a special node, a TreeBin or a ForwardingNode during a resize, and get() asks that node to find the key.
Notice that nothing in this path waits for anyone. A reader on one core and a writer on another can work on the same bin at the same time, and the reader still gets a correct answer, which is the second follow-up question, answered in detail below.
3How put() Works: Two Cases
put() spreads the hash, finds the bin, and then takes one of two paths. If the bin is empty, it creates a node and tries to install it with a CAS, compare-and-set, an atomic CPU instruction that writes the new node only if the slot is still null. If another thread installed a node first, the CAS fails and put() loops around and tries again, now finding a non-empty bin. No lock is taken in this case, which is the common one in a map that is not crowded.
If the bin is not empty, put() synchronizes on the bin's first node, re-checks that this node is still the head of the bin, and then walks the list: if it finds the key it replaces the value, otherwise it appends a new node. Only threads writing to this same bin wait for this lock. Writers to other bins, and all readers, proceed.
- Spread the hash, find the bin(n - 1) & hash
- Is the bin empty?YesCAS in a new nodeNoIs it a forwarding node?
- Forwarding node (resize running)?YesHelp move bins, then retryNoLock the first node of the bin
- Under the bin lockreplace the value or append a node
- Update the size counterbase counter or a counter cell
5 steps in order. Play them, or step through them one at a time.
Only the third path takes a lock, and only on one bin.
4Tree Bins and the Size Counter
As in HashMap, a bin whose list reaches 8 nodes is converted to a balanced tree, provided the table has at least 64 bins; in a smaller table the map resizes instead. The bin's head then becomes a TreeBin, a special node that holds the tree's root. Lookups in a crowded bin become O(log n) instead of O(n), which also protects against many keys with colliding hashes.
Counting entries is a contention problem in itself: if every put() incremented one shared field, all writers would collide on it even when they used different bins. So the map keeps a baseCount, which a thread first tries to update with a CAS, and if that CAS fails because of contention, the thread adds to one of several CounterCell objects chosen by a per-thread random probe, the same technique LongAdder uses. size() and mappingCount() add the base and the cells. The result is cheap to maintain but only an estimate while writes are happening.
5What Replaced the Java 7 Segments?
This first follow-up has a short answer: per-bin locking. Java 7 divided the map into a fixed number of Segment objects, 16 by default, each a ReentrantLock guarding its own sub-table, so at most that many writers could work in parallel. Java 8 removed segments, using CAS for empty bins and a synchronized block on the first node for others. The number of independent locks now equals the number of bins and grows as the table resizes. The old concurrencyLevel constructor argument survives only as a sizing hint.
6Why Can get() Proceed While a Bin Is Being Modified?
The second follow-up is about safe publication. A writer fully constructs a new node, with its final hash and key and its value, before linking it in with a volatile write, either the CAS on the bin slot or the write to the previous node's volatile next pointer. A reader that sees the link through a volatile read therefore sees the complete node, never a half-built one. A value update is a single volatile write to val, so the reader sees either the old or the new value. Removal unlinks a node by changing a volatile next pointer, so a reader already standing on the removed node can still follow its next pointer and finish its walk.
Tree bins need extra care, because rebalancing rotates nodes and a reader walking the tree mid-rotation could get lost. TreeBin solves this with a small read-write lock state of its own: if a writer is restructuring the tree, readers do not wait, they walk the bin's nodes as a plain linked list through the next pointers, which are kept valid, and use the tree again once the writer is done. During a resize, a reader that meets a ForwardingNode follows it to the new table. In all three cases the reader gets a correct answer for some moment during its call, without waiting.
7Follow-up questions
Why does ConcurrentHashMap reject null keys and values?
If get() returned null for a key mapped to null, a caller could not tell whether the key was absent or mapped to null, and checking with containsKey() afterwards would be a race because another thread could change the map in between. Forbidding null makes a null from get() always mean absent.
Are compound operations such as computeIfAbsent atomic?
Yes. compute, computeIfAbsent, computeIfPresent and merge run their function while holding the bin's lock, so the check and the update happen together. That is also why the function must be short and must not modify the same map, which can deadlock or throw IllegalStateException for recursive updates.
Does ConcurrentHashMap guarantee that get() sees a put() that is still running?
No. Retrievals reflect the results of the most recently completed updates when they start. A put() that has not yet made its volatile write is not visible, and one that has is. There is no partial state in between, which is the guarantee that matters.
8Conclusion
In this answer, we saw that ConcurrentHashMap avoids a global lock by combining three techniques: volatile reads of bins and of each node's value and next pointer, so get() never locks and always sees fully published nodes; a CAS to install the first node of an empty bin; and a synchronized block on the first node of a non-empty bin, so writers only block writers of the same bin. Crowded bins become TreeBins whose readers fall back to the linked list while a writer restructures, and the size lives in a base counter plus striped counter cells. Per-bin locking is what replaced the Java 7 segments.
Open with
It combines volatile reads, CAS and very fine-grained locking. The table is an array of bins, and the map reads and writes its slots with volatile semantics; inside each node, the value and the next pointer are volatile too.
Then, if they want more
So get() just reads the bin, compares the hash and key of the first node and walks the list if needed, with no lock at all. put has two cases.
If the bin is empty, it builds a node and installs it with a CAS that only succeeds if the slot is still null; if another thread won, it loops and retries.
If the bin isn't empty, it synchronizes on the first node of that bin only, re-checks it's still the head, and then replaces the value or appends a node. Writers to different bins never block each other, and readers never block. If put sees a forwarding node, a resize is running and the thread helps move bins first.
As in HashMap, a bin with eight or more nodes in a table of at least 64 becomes a TreeBin. For the size, a single counter would be a contention point, so there's a base count updated by CAS and, under contention, striped counter cells like LongAdder, summed by size or mappingCount.
This replaced Java 7's segments, which were a fixed number of ReentrantLocks over sub-tables. And get can run during a modification because writers fully build a node before linking it with a volatile write, so readers see either the old state or the complete new one; tree bins let readers walk the linked nodes while a writer rebalances.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 7, Java 8
- 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 "How does ConcurrentHashMap in Java 8 and later make put and get thread-safe without one...": https://scrivio-interview-library.netlify.app/java/concurrent-collections/d27b263ebd/
Java fundamentals, question 14 of 16 Back to the path
More in Concurrent collections