Java并发 - ConcurrentMap接口

java.util.concurrent.ConcurrentMap接口是Map接口的子接口,支持底层映射变量的原子操作.它具有get和set方法,类似于对volatile变量的读写操作.也就是说,一个集合与之前的相关变量的任何后续获取具有先发生关系.此接口可确保线程安全性和原子性保证.

ConcurrentMap方法

Sr.No.方法&描述
1

default V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

尝试计算a映射指定键及其当前映射值(如果没有当前映射,则为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)

如果值为指定的键存在且非空,尝试计算给定键a的新映射及其当前的映射值.

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关联,将其与给定的非空值相关联.

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的用法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)