Java 并发 - ConcurrentMap 接口
java.util.concurrent.ConcurrentMap接口是Map接口的子接口,支持对底层map变量的原子操作。 它具有 get 和 set 方法,其工作方式类似于对易失性变量进行读取和写入。 也就是说,集合与同一变量上的任何后续获取具有先发生关系。 该接口保证了线程安全和原子性保证。
ConcurrentMap 方法
| Sr.No. | 方法及描述 |
|---|---|
| 1 | default V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) 尝试计算指定键及其当前映射值的映射(如果没有当前映射,则为 null)。 |
| 2 | default V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) 如果指定的键尚未与值关联(或映射为 null),则尝试使用给定的映射函数计算其值并将其输入到此映射中,除非 null。 |
| 3 | default V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) 如果指定键的值存在且非空,则尝试计算给定键及其当前映射值的新映射。 |
| 4 | default void forEach(BiConsumer<? super K,? super V> action) 对此映射中的每个条目执行给定的操作,直到处理完所有条目或该操作引发异常。 |
| 5 | default V getOrDefault(Object key, V defaultValue) 返回指定键映射到的值,如果此映射不包含该键的映射,则返回 defaultValue。 |
| 6 | default V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) 如果指定的键尚未与值关联或与 null 关联,则将其与给定的非 null 值关联。 |
| 7 | V putIfAbsent(K key, V value) 如果指定的键尚未与值关联,则将其与给定值关联。 |
| 8 | boolean remove(Object key, Object value) 仅删除当前映射到给定值的键条目。 |
| 9 | V replace(K key, V value) 仅当当前映射到某个值时才替换键的条目。 |
| 10 | boolean replace(K key, V oldValue, V newValue) 仅当当前映射到给定值时才替换键的条目。 |
| 11 | default void replaceAll(BiFunction<? super K,? super V,? extends V> function) 用在该条目上调用给定函数的结果替换每个条目的值,直到处理完所有条目或函数引发异常。 |
示例
以下 TestThread 程序显示了 ConcurrentMap 与 HashMap 的用法。
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class TestThread {
public static void main(final String[] arguments) {
Map<String,String> map = new ConcurrentHashMap<String, String>();
map.put("1", "One");
map.put("2", "Two");
map.put("3", "Three");
map.put("5", "Five");
map.put("6", "Six");
System.out.println("Initial ConcurrentHashMap: " + map);
Iterator<String> iterator = map.keySet().iterator();
try {
while(iterator.hasNext()) {
String key = iterator.next();
if(key.equals("3")) {
map.put("4", "Four");
}
}
} catch(ConcurrentModificationException cme) {
cme.printStackTrace();
}
System.out.println("ConcurrentHashMap after modification: " + map);
map = new HashMap<String, String>();
map.put("1", "One");
map.put("2", "Two");
map.put("3", "Three");
map.put("5", "Five");
map.put("6", "Six");
System.out.println("Initial HashMap: " + map);
iterator = map.keySet().iterator();
try {
while(iterator.hasNext()) {
String key = iterator.next();
if(key.equals("3")) {
map.put("4", "Four");
}
}
System.out.println("HashMap after modification: " + map);
} catch(ConcurrentModificationException cme) {
cme.printStackTrace();
}
}
}
这将产生以下结果。
输出
Initial ConcurrentHashMap: {1 = One, 2 = Two, 3 = Three, 5 = Five, 6 = Six}
ConcurrentHashMap after modification: {1 = One, 2 = Two, 3 = Three, 4 = Four, 5 = Five, 6 = Six}
Initial HashMap: {1 = One, 2 = Two, 3 = Three, 5 = Five, 6 = Six}
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(Unknown Source)
at java.util.HashMap$KeyIterator.next(Unknown Source)
at TestThread.main(TestThread.java:48)

