CKTIS / Java
ConcurrentHashMap: get, put, and atomic merge
Use `get` to read, `put` to write unconditionally, and `merge` to update one key atomically, for example when threads share a counter.
Version: Java SE 21
Syntax
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
// Instance methods of ConcurrentHashMap<K, V>
public V get(Object key)
public V put(K key, V value)
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)In Java SE 21, ConcurrentHashMap.get (get(Object key)) reads a value without changing the map and returns null if the key is absent. ConcurrentHashMap.put inserts or replaces a value and returns the previous value, or null if there was no entry.
ConcurrentHashMap.merge inserts the supplied value if the key is absent, without calling the function. If the key is present, it calls the function with the existing value first and the supplied value second. It stores and returns the result, or removes the entry and returns null if the function returns null.
The map stores neither null keys nor null values. A null key in any of these methods, a null value passed to put or merge, or a null function passed to merge causes NullPointerException. If the function throws a RuntimeException or an Error, merge propagates it without changing the entry. Keep the function short: some updates may have to wait for it to finish. The remapping function must not modify this map during computation. If merge detects an attempted recursive update to this map that would otherwise never complete, it throws IllegalStateException.
One merge call reads and updates one key atomically. Separate get and put calls can lose an increment if two threads read the same value and write the same result. This guarantee belongs to ConcurrentHashMap, not the default Map.merge implementation, and it does not provide a transaction across multiple keys.
Examples
import java.util.concurrent.ConcurrentHashMap;
public class Main {
public static void main(String[] args) throws InterruptedException {
var counts = new ConcurrentHashMap<String, Integer>();
String key = "hits";
System.out.println(counts.get(key));
System.out.println(counts.merge(key, 1, Integer::sum));
System.out.println(counts.put(key, 10));
Runnable increment = () -> {
for (int i = 0; i < 1_000; i++) {
counts.merge(key, 1, Integer::sum);
}
};
Thread first = new Thread(increment);
Thread second = new Thread(increment);
first.start();
second.start();
first.join();
second.join();
System.out.println(counts.get(key));
System.out.println(counts.merge(key, 1, (oldValue, addedValue) -> null));
System.out.println(counts.get(key));
}
}Result
null 1 1 2010 null null
The first read returns `null`. The first `merge` inserts and returns `1`, and `put` stores `10` and returns the old `1`. Each thread adds 1000; `join()` waits for both threads to finish before the program reads `2010`. The final `merge` removes the entry, so both that call and the next read return `null`.
Related lessons
- Java · Lesson 56 · Intermediate · Version: 2