Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

Explain how ConcurrentHashMap resizes while other threads keep reading and writing.

When the size passes the threshold, about 0.75 times the table length, ConcurrentHashMap allocates a table twice as large and moves bins into it in ranges that threads claim one stride at a time. Each moved bin is replaced in the old table by a ForwardingNode that points to the new table, so readers that hit it simply continue there, and writers that hit it help with the transfer instead of waiting. A bin is locked only while it is being split, so writes to that one bin wait briefly while reads never do, and pre-sizing the map with the expected number of entries avoids repeated resizes under load.

Why this question is asked

Resizing is where a concurrent hash map is hardest to get right. A plain HashMap simply builds a new array and moves everything while nobody else is allowed to look, but ConcurrentHashMap has to keep serving reads and writes on a structure that is being rebuilt underneath them. Interviewers ask this to see whether we understand forwarding nodes and cooperative transfer, and whether we can turn that into practical advice. In this answer, we will see what triggers a resize, how the work is divided, what readers and writers do when they meet a moved bin, how a single bin is split, and why pre-sizing matters.

1When a Resize Starts

ConcurrentHashMap keeps a control field called sizeCtl. While the map is in normal use, it holds the next resize threshold, which is 0.75 times the table length: 12 for a table of 16, 768 for a table of 1,024. After each insertion, the thread that updated the size counter checks whether the count has reached sizeCtl. If it has, that thread starts a resize: it allocates a new table of twice the length, publishes it as nextTable, and sets sizeCtl to a negative value that records that a resize is in progress and how many threads are working on it.

The old table stays fully in use. Nothing is copied all at once; bins move one at a time, and until a bin moves, it is read and written in the old table as usual.

2Dividing the Work Between Threads

The bins are handed out in ranges called strides. A shared counter, transferIndex, starts at the old table's length, and each thread that joins the transfer claims the next stride by lowering transferIndex with a CAS, then moves those bins from the highest index down. The stride depends on the table size and the number of CPUs, with a minimum of 16 bins, so a large table is split into many chunks that several threads can process in parallel.

Step throughA resize of a 1,024-bin table
  1. Size reaches 7680.75 x 1,024: the inserting thread starts the resize
  2. Allocate nextTable of 2,048 binssizeCtl turns negative: resize running
  3. Threads claim strides from transferIndexe.g. bins 1008-1023, then 992-1007
  4. Move each bin, leave a ForwardingNodeold slot now points to the new table
  5. All strides done?
    YesLast thread swaps in the new tableNoKeep claiming strides
  6. New threshold 1,536normal operation on 2,048 bins

6 steps in order. Play them, or step through them one at a time.

Bins move one at a time; the map stays usable throughout.

When a thread finishes its stride, it claims another, until transferIndex reaches 0. The last thread to finish rechecks the whole old table, then installs the new table as the map's table and sets sizeCtl to the new threshold, 0.75 times the new length. From then on, all operations use the new table directly.

3ForwardingNode: Readers Continue, Writers Help

When a bin has been moved, the old table's slot is set to a ForwardingNode. It is a special node with the hash value MOVED, a negative number no real node can have, and a reference to the new table. An empty bin is marked the same way with a CAS, so even empty slots tell everyone that they now live in the new table.

Java
// what each operation does when it meets a ForwardingNode in the old table
get(key): e.hash == MOVED -> fwd.find(hash, key) // look up in nextTable, no waiting
put(key, v): f.hash == MOVED -> helpTransfer(tab, f) // claim a stride, move bins, retry
iteration: sees MOVED -> visit the bin's two new slots in nextTable instead

The reader's case is simple: it follows the pointer and looks up the key in the new table, where the bin's nodes already are. The writer's case is the clever part. Instead of waiting for the resize to finish, a writer that runs into a ForwardingNode joins the transfer, claims a stride, moves bins, and then retries its put in the new table. So the more threads are writing, the more threads share the resize work, and a write-heavy map finishes its resize faster rather than piling up waiters. The iterators handle forwarding nodes too, which is how they keep their weakly consistent guarantee of returning each entry at most once across a resize.

4Splitting One Bin

Moving a non-empty bin means splitting it. Because the new table is exactly twice as long, a node at index i in the old table goes either to index i or to index i + n in the new one, depending on a single bit of its hash, the bit worth n. The transferring thread locks the bin's first node, the same lock put() uses, divides the nodes into a low list and a high list, installs them at i and i + n in the new table, and finally sets the ForwardingNode in the old slot.

Java
// old table n = 16, bin 5 holds hashes 5, 21, 37, 53
// 5 & 16 = 0 -> low list -> new bin 5
// 21 & 16 = 16 -> high list -> new bin 21
// 37 & 16 = 0 -> low list -> new bin 5
// 53 & 16 = 16 -> high list -> new bin 21
// new table bin 5: [5, 37] bin 21: [21, 53] old bin 5: ForwardingNode

Notice what this means for the other threads. A writer to bin 5 waits on the bin lock for the few microseconds the split takes, and writes to every other bin carry on. A reader never waits: the split builds new nodes for the new table, reusing an unchanged tail of the list where it can, and does not modify the old nodes' next pointers, so a reader already walking the old bin finishes on a consistent list, and a reader arriving after the ForwardingNode is set goes to the new table. Tree bins are split the same way, and a half that becomes small enough, 6 nodes or fewer, is turned back into a plain list.

5Avoiding Resizes by Pre-Sizing

Resizing is safe, but it is not free. Each resize allocates a new array and touches every entry, and growing a map from 16 bins to over a million takes 17 doublings, with the threads doing the inserts paying for the transfers while latency spikes. If we know roughly how many entries the map will hold, we can create it with that size.

Java
// grows 16 -> 32 -> ... -> 2,097,152 bins while filling: 17 resizes
Map<Long, Account> grown = new ConcurrentHashMap<>();
// sized up front: the table already fits 1,000,000 entries, no resizes while filling
Map<Long, Account> sized = new ConcurrentHashMap<>(1_000_000);

Note a difference from HashMap: ConcurrentHashMap's initialCapacity constructor argument is documented as the number of elements the map should accommodate without resizing, so we pass the expected count directly, while HashMap's constructor takes a raw table capacity and needs the HashMap.newHashMap(expected) factory from Java 19 to size by element count. A map that is filled at startup, such as a cache warmed from a database, benefits the most.

6Follow-up questions

Can two resizes happen at the same time?

No. Only one transfer runs at a time. sizeCtl records the resize in progress, so a thread that would start a second one sees the negative value and helps with the current transfer instead. A new resize can start only after the new table has been installed.

Does the table ever shrink?

No. ConcurrentHashMap never reduces its table, even after clear() or after most entries are removed. If a map grows to millions of entries temporarily, replacing it with a new map is the only way to release the table's memory.

Why does a reader never see an entry missing during a move?

Until the ForwardingNode is set, the old bin still holds all its nodes, and the new bins are complete before it is set. The volatile write of the ForwardingNode is the moment the bin moves, so a reader sees either the full old bin or a pointer to the full new bins, never a half-moved state.

7Conclusion

In this answer, we saw that ConcurrentHashMap starts a resize when the size reaches about 0.75 times the table length, allocates a table twice as large, and moves bins in strides that threads claim from a shared counter. Each moved bin leaves a ForwardingNode, so readers continue in the new table and writers help with the transfer instead of waiting. Splitting a bin sends each node to index i or i + n under that bin's lock, so only writes to that bin pause briefly and reads never do. Creating the map with the expected number of entries avoids repeated resizes under load.

What to say in the interviewAbout 110 seconds aloud

Open with

A resize starts when the size counter reaches sizeCtl, which is about three quarters of the table length. The thread that crosses it allocates a new table of double the size and marks a resize as running.

Then, if they want more

Bins are then moved in ranges: a shared transferIndex starts at the old length, and each thread claims a stride of bins, at least 16, with a CAS and moves them from high to low.

When a bin is moved, its slot in the old table is replaced with a ForwardingNode, a special node with a MOVED hash that points to the new table. Empty bins get one by CAS. So a reader that hits a ForwardingNode just continues its lookup in the new table, without waiting.

A writer that hits one doesn't wait either; it calls helpTransfer, claims a stride, moves bins, and then retries its put in the new table, so heavy write traffic actually speeds the resize up.

To move a non-empty bin, the thread locks its first node, the same lock put uses, and splits the nodes by one hash bit into a list for index i and one for i plus n.

Writes to that one bin wait for a few microseconds, but reads don't, because the old nodes aren't modified and the forwarding node is only set once the new bins are complete. The last thread installs the new table and the new threshold.

In practice, if I know a map will hold a million entries, I create it with that initial capacity, which ConcurrentHashMap treats as the number of elements to fit without resizing, so it never goes through the 17 doublings.

About this answer

Last updated
12 November 2026
Versions it names
Java 19
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 "Explain how ConcurrentHashMap resizes while other threads keep reading and writing.": https://scrivio-interview-library.netlify.app/java/concurrent-collections/175976073d/