Skip to content
Ameen Abozaid
Planned lessonMID-005-L02

ConcurrentHashMap

  • Lesson 2 of 5
  • Mid-level
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, merge are atomic per key.
  • Keep the function passed to compute/merge short, side-effect free, and non-blocking; do not update the same map inside it.
  • No null keys 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

java
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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.