Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What were segments in Java 7's ConcurrentHashMap, what replaced them in Java 8, and what does the concurrencyLevel constructor argument do today?

In Java 7, ConcurrentHashMap was split into a fixed array of Segment objects, by default 16, each a ReentrantLock guarding its own small hash table, so at most as many writers as there were segments could proceed at once, and that number was fixed at construction. Java 8 removed segments and locks individual bins instead, using CAS to fill an empty bin and synchronizing on the first node otherwise. Today the concurrencyLevel constructor argument is only a sizing hint for the initial capacity, and a stripped-down Segment class remains only for serialization compatibility.

Why this question is asked

ConcurrentHashMap is the default concurrent map in Java, and its internals changed completely between Java 7 and Java 8. Many tutorials still describe segments, so interviewers ask this question to see whether our knowledge is current and whether we understand why the design changed. The old design is also a good lesson in lock striping and its limits. In this answer, we will look at how segments worked, what limited them, what Java 8 put in their place, and what is left of concurrencyLevel and Segment today.

1A Quick Recap: Why Not One Lock?

The simplest thread-safe map wraps a HashMap with one lock, as Collections.synchronizedMap and Hashtable do. Every get and put takes that lock, so on a machine with 32 cores, only one thread at a time can touch the map, and everyone else waits.

The idea behind ConcurrentHashMap from its first version in Java 5 was lock striping: split the data into parts and give each part its own lock, so threads working on different parts do not block each other. Segments were the Java 5 to Java 7 implementation of that idea.

2How Segments Worked in Java 7

A Java 7 ConcurrentHashMap held an array of Segment objects. Each Segment extended ReentrantLock and contained its own hash table of entries, its own element count and its own resize threshold; in effect, each segment was a small, separately locked hash map. The number of segments was the smallest power of two at least as large as the concurrencyLevel argument, which defaulted to 16.

Java
// Java 7 shape, simplified
final Segment<K,V>[] segments; // 16 by default, fixed forever
static final class Segment<K,V> extends ReentrantLock {
transient volatile HashEntry<K,V>[] table; // this segment's own buckets
transient int count;
}
// put(key, value):
// hash = spread(key.hashCode())
// segment = segments[(hash >>> segmentShift) & segmentMask] // high bits pick the segment
// segment.lock(); insert into segment.table at (hash & (table.length - 1)); unlock()
Step throughJava 7 ConcurrentHashMap
  1. Segment arrayfixed size, 16 by default, chosen from concurrencyLevel
  2. Segment = ReentrantLockone lock per segment, taken by writers
  3. Per-segment tableHashEntry buckets, resized on their own
  4. HashEntry chainslinked lists of key-value entries

4 layers, from the top down. Step through them one at a time.

Reads were mostly lock-free, relying on volatile fields, and only writes locked their segment. Resizing also happened per segment: when one segment's table filled up, only that segment doubled, under its own lock, while the other fifteen kept working.

3What Limited the Segment Design

The number of segments was decided at construction and could never change. With the default of 16, at most 16 threads could write at the same time, and only if their keys happened to fall into different segments. On a 64-core server with heavy writes, threads still queued behind segment locks. Choosing a much larger concurrencyLevel helped writers but cost memory, since each segment carried its own object, lock and table, and made whole-map operations slower.

Those whole-map operations were the other weakness. size() first tried summing the segment counts twice without locking, and if writers kept changing them, it locked all the segments to get a stable count, briefly stopping every writer. So the design traded fine-grained writing for awkward global operations.

4What Replaced Segments in Java 8

Java 8 rewrote the class. There is now a single table of bins, like HashMap, and the lock granularity is one bin. Putting into an empty bin needs no lock at all: the thread installs the new node with a CAS, a compare-and-set instruction that succeeds only if the bin is still empty. Putting into a non-empty bin synchronizes on that bin's first node, so only threads writing to the same bin wait for each other.

With a table of, say, 1,024 bins, there are effectively 1,024 locks, and their number grows as the table resizes, so concurrency scales with the map instead of being fixed at construction. The intrinsic lock costs nothing in memory until it is contended, unlike a ReentrantLock object per segment. Size is tracked with striped counters, similar to LongAdder, so even size() no longer needs to lock anything.

Java 7 segments versus Java 8 bins
AspectJava 7Java 8 and later
Lock unitSegment (ReentrantLock)First node of a bin (synchronized)
Number of locksFixed, 16 by defaultGrows with the table
Empty bin insertTakes the segment lockCAS, no lock
ResizingPer segmentWhole table, threads help
size()May lock all segmentsSums striped counters, no lock

5ConcurrencyLevel and Segment Today

The three-argument constructor ConcurrentHashMap(initialCapacity, loadFactor, concurrencyLevel) still exists, because removing it would break source compatibility. But concurrencyLevel no longer creates anything. The Javadoc describes it as the estimated number of concurrently updating threads, used as a sizing hint, and the implementation uses it only to make sure the initial capacity is at least that large.

Java
var a = new ConcurrentHashMap<String, Integer>(16, 0.75f, 64);
// Java 8+: behaves like an initial capacity of at least 64; no segments created
var b = new ConcurrentHashMap<String, Integer>(1_000);
// the usual way: the table is sized so 1,000 entries fit without resizing

The Segment class itself survives as a stripped-down, private nested class. Its only job is serialization: a ConcurrentHashMap written to a stream by Java 8 or later still includes a segments field in the format Java 7 expected, and reading an old stream still works. No live map ever uses it.

6Follow-up questions

Was Java 7's get() locking?

Normally no. Entries and tables were read through volatile fields, so get() could usually find a value without the segment lock. The lock was for writes and for a rare retry path in early versions.

Why did Java 8 use synchronized instead of ReentrantLock for bins?

Each bin needs a lock only occasionally, and the first node already exists, so synchronizing on it costs no extra memory. An uncontended synchronized block is cheap in modern JVMs, while a ReentrantLock per bin would be an object per bin. The bin lock is also held only for the short time it takes to update one bin.

Should we still pass a concurrencyLevel?

There is no need. Pass the expected number of entries as the initial capacity instead, which is what actually avoids resizing. Passing concurrencyLevel does no harm but also does nothing beyond raising the capacity.

7Conclusion

In this answer, we saw that Java 7's ConcurrentHashMap used lock striping through a fixed array of Segment objects, 16 by default, each a ReentrantLock with its own small table, so write concurrency was capped by the segment count chosen at construction and whole-map operations such as size() could lock everything. Java 8 replaced segments with per-bin locking: CAS for an empty bin, synchronized on the first node otherwise, and striped counters for the size. concurrencyLevel is now only a hint for the initial capacity, and Segment remains only so that serialized maps stay compatible.

What to say in the interviewAbout 90 seconds aloud

Open with

In Java 7, ConcurrentHashMap used lock striping through segments. The map held a fixed array of Segment objects, 16 by default, and each segment extended ReentrantLock and had its own little hash table with its own count and threshold, so it was like 16 separately locked hash maps.

Then, if they want more

The high bits of the hash picked the segment and the low bits picked the bucket inside it. Reads were mostly lock-free through volatile fields, writes locked only their segment, and each segment resized on its own.

The limitation was that the number of segments came from concurrencyLevel at construction and never changed, so at most that many writers could proceed at once, and only if their keys landed in different segments. Global operations like size could end up locking every segment. Java 8 removed segments completely.

There's one table of bins like HashMap, inserting into an empty bin is a CAS with no lock, and inserting into a non-empty bin synchronizes only on that bin's first node. So the number of effective locks grows with the table, and size uses striped counter cells like LongAdder instead of locks.

Today the concurrencyLevel argument is only a sizing hint: the constructor makes sure the initial capacity is at least that big, nothing more. And a stripped-down Segment class is still there only so serialized maps stay compatible with the old format.

About this answer

Last updated
12 November 2026
Versions it names
Java 7, Java 8, Java 5
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 "What were segments in Java 7's ConcurrentHashMap, what replaced them in Java 8, and what...": https://scrivio-interview-library.netlify.app/java/concurrent-collections/4707e50000/