目录

并发 - ConcurrentMap( 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)

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

如果指定键的值存在且为非null,则尝试在给定键及其当前映射值的情况下计算新映射。

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)

将每个条目的值替换为在该条目上调用给定函数的结果,直到所有条目都已处理或函数抛出异常。

例子 (Example)

以下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();
      }
   }  
}

这将产生以下结果。

输出 (Output)

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)
↑回到顶部↑
WIKI教程 @2018