ConcurrentHashMap solves concurrency problems in HashMap by using a segment-based locking mechanism (pre-Java 8) where each segment has its own lock, allowing parallel access to different segments, and in Java 8+ by replacing segments with Compare-and-Swap (CAS) operations for lock-free reads and bucket-level synchronization, enabling high concurrency without locking the entire map.
Deep Dive
Voraussetzung
- Keine Daten verfügbar.
Nächste Schritte
- Keine Daten verfügbar.
Deep Dive
Why concurrent Hashmap?? | Concurrent Hashmap Internals #shorts #concurrenthashmap #hashmap #javaHinzugefügt:
Internal working of concurrent hashmap.
How is it different from hashmap and what is the concurrency model that it uses under the under the hood? So, hashmap works great in a single-threaded scenarios and but under concurrency it can lead to race conditions, inconsistent state and performance degradation. Con- Concurrent hashmap is designed to solve these problems not by simply adding locks, but by rethinking the internal architecture for the scalability in here.
Why not we synchronize the hashmap directly like the way that we have map.collections.synchronizedMap and we can pass the hashmap and that's how it will be uh thread safe, right? But there is a problem. Entire map is locked for every operation, no parallelism, high contention under load. So, how concurrent hashmap solves that one pre-Java 8. Map is divided into segments and each segment had its own lock. You see, the concurrent map has different segments there and each of the segment has its own lock. So, if right is happening, so if that is happening on that segment only and that will be locked, not other segments. They will be free to read.
But there is a limitation. Concurrency is limited by the number of segments, right? But what is the improvement that has happened around the Java 8 plus?
Segments removed and they replaced they got replaced with the compare and swap.
That is the mechanism CAS. Bucket level synchronization is there. Lock free uh lock free reads are there now.
And this is how the internal data structure looks like. Transient, volatile, then we have node of the key and value. That's how the table is there.
Each bucket can contain node linked list or tree bin red black tree forwarding node during resizing. High level architecture looks like this one. So, concurrent hashmap table is there. The representation that we have seen this table in here, right? So, this will go like this one. Uh bucket 0 1 2. Each bucket will be locked at a time.
Put operation internals. when you do the map.put in here, hash calculation happens. That is the first thing in here. Bucket indexing is going to happen. Insert logic is going to happen.
That is where the CAS, compare and swap, no locking will happen in here. So, non-empty bucket in here, this is how it is going to do that one. This is how it is going to handle the collision in here. Bucket node one, node two in here.
After threshold, the tree bin will be converted like the instead of linked list, that will be converted into the a tree bin in here. So, treefication happens in here. Resizing measure in here. So, this is how you can see the difference between the hash map and concurrent hash map. And huge difference and very important question for the interviews.
Ähnliche Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











