java.util.concurrent.ConcurrentMap接口是Map接口的子接口,支持对基础map变量的原子操作。它具有设置方法,该方法的工作方式类似于对易失性变量的读写。就是说,一个集合与同一变量的任何后续get都具有事前发生的关系。该接口确保线程安全性和原子性保证。
| 序号 | 方法与说明 |
|---|---|
| 1 | 默认V计算(K键,BiFunction <?super K ,? super V ,?扩展V> remappingFunction) 尝试计算指定键及其当前映射值的映射(如果没有当前映射,则为null)。 |
| 2 | 默认的V validateIfAbsent(K键,Function <?超级K ,?扩展V>映射函数) 如果指定的键尚未与值关联(或映射为null),则尝试使用给定的映射函数计算其值,并除非为null,否则将其输入此映射。 |
| 3 | 默认VcomputeIfPresent(K键,BiFunction <?super K,?super V,?extended V> remappingFunction) 如果存在指定键的值且该值不为null,则尝试计算给定键及其当前映射的新映射值。 |
| 4 | 默认void forEach(BiConsumer <?super K,?super V> action) 对此映射中的每个条目执行给定的操作,直到已处理所有条目或该操作引发异常。 |
| 5 | default V getOrDefault(Object key,V defaultValue) 返回指定键所映射到的值,如果此映射不包含该键的映射关系,则返回defaultValue。 |
| 6 | 默认V merge(K键,V值,BiFunction <?super V,?super V,?extended V> remappingFunction) 如果指定的键尚未与值关联或与null关联,则将其与给定的非键关联。空值。 |
| 7 | V putIfAbsent(K键,V值) 如果指定的键尚未与值关联,则将其与给定的值关联。 |
| 8 | booleanremove(Object key,Object value) 仅在当前映射到给定值时才删除键的条目。 |
| 9 | V replace(K键,V值) 仅在当前映射到某个值时才替换键的条目。 |
| 10 | boolean replace(K key,V oldValue,V newValue) 仅在当前映射到给定值时才替换键的条目。 |
| 11 | 默认void replaceAll(BiFunction <?super K ,? super V,?extends V> function) 用在该条目上调用给定函数的结果替换每个条目的值,直到处理完所有条目或该函数引发异常为止。 |
The following TestThread program shows usage of ConcurrentMap vs 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)