Ch. 7 · Java

Java Concurrent Collections and Compound Operations

Java Concurrent Collections and Compound Operations. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

Thread-safe methods do not automatically make a sequence of calls atomic. Concurrent collections often provide combined operations for common invariants.

Before you start

You should know Java classes, methods, collections and exceptions. Trace object identity separately from the contents of an object. For concurrent examples, state which thread owns or shares the data; a single successful execution is not proof that every interleaving is safe.

The practical goal is to reason through this situation: Use putIfAbsent rather than separate containsKey and put calls. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Recognize compound actions

ContainsKey followed by put has an interleaving between the methods.

Step 2: Use atomic map operations

Choose putIfAbsent or an appropriate compute operation for the required transition.

Step 3: Understand callback constraints

Keep map callbacks bounded and follow the documented concurrency and recursion rules.

Worked scenario

Use putIfAbsent rather than separate containsKey and put calls.

Threads A and B both observe a missing key before either puts its value. Both then initialize it despite individually thread-safe methods. A combined operation closes that check-update race, but external side effects in initialization still need careful ownership and cannot be treated as universal exactly-once work.

Common mistake

Two individually safe calls can race between the check and update.

Verify the behavior

Coordinate two simultaneous initializers and inspect both stored values and side effects.

Interview exercise

Initialize a cached value once logically.

Answer and reasoning

Use a suitable atomic map operation and understand its callback restrictions and possible repeated computation behavior.

Continue learning

Compare the scenario with the Java interview questions and test your understanding with the Java MCQs. For terminology and implementation details, consult the reference material.

More in Java

read ✓Java · hard

Java Time Types and Zone-Aware Decisions

Java Time Types and Zone-Aware Decisions. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readread →
read ✓Java · hard

Java Generics Wildcards and PECS

Use ? extends T for producers and ? super T for consumers, and understand why List<String> is not a List<Object>.

~2 min readread →
read ✓Java · easy

Java ArrayList Capacity and Performance

Java ArrayList Capacity and Performance. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readread →
esc