Java中故障快速和故障安全之间的区别
故障保护示例
public class FailSafeExample{
public static void main(String[] args){
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String, Integer>();
//在映射上添加元素
map.put("Dell", 1);
map.put("IBM", 2);
//从映射获取迭代器
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()){
String key = (String) it.next();
System.out.println(key+" : "+map.get(key));
map.put("Google", 3);
}
}
}输出结果
IBM :2 Dell:1
故障保护示例
public class FailFastExample{
public static void main(String[] args){
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
//从列表中获取迭代器
Iterator<Integer> it = list.iterator();
while (it.hasNext()){
Integer integer = (Integer) it.next();
list.add(4);
}
}
}输出结果
Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿