ConcurrentHashMap
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
use atomic map operations correctly.
Planned topics
- Fine-grained internal locking and lock-free reads allow high concurrency.
putIfAbsent,computeIfAbsent,compute,mergeare atomic per key.- Keep the function passed to
compute/mergeshort, side-effect free, and non-blocking; do not update the same map inside it. - No
nullkeys or values. size()and iteration are weakly consistent; they give a snapshot-like view, not a frozen one. Do not use them for exact decisions.
Draft outline details
Demonstration
Map<String, LongAdder> views = new ConcurrentHashMap<>();
views.computeIfAbsent(sku, k -> new LongAdder()).increment();
Map<String, Integer> stock = new ConcurrentHashMap<>();
stock.merge(sku, 1, Integer::sum);Check your understanding
Which call replaces get-then-put counting?
Show answer
merge(key, 1, Integer::sum).
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.