Java缓存使用ArrayList/HashMap的常见问题----ConcurrentModification
来源:互联网 发布:linux一键安装脚本 编辑:程序博客网 时间:2024/06/07 00:02
对于一些经常读取,但是很少写的数据,经常会使用缓存来存储。常见的做法是:
List<String> list = new ArrayList<String>();Map<String,String> map = new HashMap<String,String>();//if modifypublic void modify() { synchronized(list){ //do modify list } synchronized(map){ //do modify map }}
这样的做法会有一些风险。因为list/map可能在另一个地方正在被迭代使用,比如:
Iterator<String> it = list.iterator();while(it.hasNext()){ //do things}for(String str : it){ //do things}
这个时候,会立即报ConcurrentModification的错误,而且错误发生的概率比较小,但是一旦出错,就会造成严重错误以致宕机,因为你肯定不会在迭代的地方try/catch该异常。原因是ArrayList和hashmap这些jdk1.5以下的集合使用的都是强一致性迭代器,见jdk描述:
he iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModification
.
然后它的建议是使用:
List list = Collections.synchronizedList(new ArrayList(...));任何时候都有可能使用当前集合的迭代器,所以必须在初始化的时候就同步好所有方法,但这样不是就等于退化到Hashtable了吗?其实就是没有高效的解决办法。
为了解决这个问题,在jdk1.5以后的concurrent集合里面,所有的实现都使用了弱一致性迭代器,不会抛ConcurrentModification错误。
所以,尽量使用ConcurrentHashMap和CopyOnWriteArrayList。他们使用了细粒度锁或者直接读写分离,非常适合读多写少的场景。
0 0
- Java缓存使用ArrayList/HashMap的常见问题----ConcurrentModification
- ArrayList &HashMap&Vector的使用
- HashMap的使用及常见问题
- java.util.ConcurrentModification…
- java.util.ConcurrentModification…
- HashMap、ArrayList、SimpleAdapter的综合使用
- Java中ArrayList与HashMap的遍历
- Java中ArrayList与HashMap的遍历
- java的HashMap和ArrayList比较器
- java hashMap, ArrayList
- Java容器类List、ArrayList、Vector及map、HashTable、HashMap的使用与区别
- Java容器类List、ArrayList、Vector及map、HashTable、HashMap的使用与区别
- java 容器类使用 Collection,Map,HashMap,hashTable,TreeMap,List,Vector,ArrayList的区别
- 熟练使用Arrays-数组-ArrayList-HashMap等常用Java类型的方法
- java 容器类使用 Collection,Map,HashMap,hashTable,TreeMap,List,Vector,ArrayList的区别
- java的ArrayList使用
- java的ArrayList使用
- java的ArrayList使用
- 清除solrcloud 中solr admin 留下的collection 无用collection
- NSOperation
- android paint api drawText 绘制字体字形
- EditPlus等编辑器选中列块的方法
- Deformable Parts Model (DPM) 简介
- Java缓存使用ArrayList/HashMap的常见问题----ConcurrentModification
- ASP.net Reuqest请求常用操作
- 239 判断一个数的所有因数的个数是偶数还是奇数
- nyoj-搜索-吝啬的国度
- 最近的android项目开发问题总结
- 利用反射获取Camera支持尺寸大小
- U3d 入门
- IOS开发笔记之开发的一些技巧
- 9.3(2)海洋之心