Java并发 专题
您的位置:java > Java并发专题 > Java并发ConcurrentMap接口
Java并发ConcurrentMap接口
作者:--    发布时间:2019-11-20

java.util.concurrent.concurrentmap接口是map接口的子接口,支持底层map变量上的原子操作。 它具有获取和设置方法,如在变量上的读取和写入。 也就是说,一个集合与同一变量上的任何后续读取相关联。 该接口确保线程安全性和原子性保证。

concurrentmap接口中的方法

序号 方法 描述
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相关联,则将其与给定的非空值相关联。
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接口的使用。

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)

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册