Java三种会抛ConcurrentModificationException异常的代码实例

在使用list集合的时候,可能会抛这个异常。具体分析,可能发生这个异常的情况有以下三种情况。我分别做了测试。

1:使用迭代器迭代的时候,集合结构遭到修改

2:for each 删除集合元素

3:多线程并发操作一个集合时候

具体都在下面测试代码里。一并都给测了。

[java]  view plain  copy
  1. package com.lxk.ConcurrentModificationExceptionTest;  
  2.   
  3. import com.google.common.collect.Lists;  
  4.   
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7.   
  8. /** 
  9.  * ConcurrentModificationException的出现情况 
  10.  * <p> 
  11.  * Created by lxk on 2016/11/11 
  12.  */  
  13. public class ConcurrentModificationExceptionTest {  
  14.     public static void main(String[] args) {  
  15.         modCountTest();  
  16.     }  
  17.   
  18.     /** 
  19.      * modCount相关测试 
  20.      */  
  21.     private static void modCountTest() {  
  22.         List<String> list = Lists.newArrayList();  
  23.         setListData(list);//给list设值:{0-9}  
  24.         System.out.println("原来未修改集合" + list);  
  25.         //wrongWay0(list);  //1:使用迭代器迭代的时候,集合结构遭到修改  
  26.         //rightWay1(list);  //  for循环删除安全  
  27.         //wrongWay1(list);  //2:for each 删除集合元素  
  28.         wrongWay2(list);    //3:多线程并发操作一个集合时候  
  29.         System.out.println("被修改过的集合" + list);  
  30.     }  
  31.   
  32.     /** 
  33.      * 获得一个list集合 
  34.      */  
  35.     private static void setListData(List<String> list) {  
  36.         for (int i = 0; i < 10; i++) {  
  37.             list.add(i + "");  
  38.         }  
  39.     }  
  40.   
  41.     /** 
  42.      * 第一种:会抛ConcurrentModificationException的异常方法 
  43.      */  
  44.     private static void wrongWay0(List<String> list) {  
  45.         Iterator<String> iterator = list.iterator();  
  46.         while (iterator.hasNext()) {  
  47.             String temp = iterator.next();  
  48.             if (iterator.next().equals("5")) {  
  49.                 //iterator.remove();//正确移除集合元素姿势  
  50.                 list.remove(temp);//抛异常移除集合元素姿势  
  51.             }  
  52.         }  
  53.     }  
  54.   
  55.     /** 
  56.      * 第二种:会抛ConcurrentModificationException的异常方法 
  57.      */  
  58.     private static void wrongWay1(List<String> list) {  
  59.         for (String s : list) {  
  60.             if ("5".equals(s)) {  
  61.                 list.remove(s);  
  62.             }  
  63.         }  
  64.     }  
  65.   
  66.     /** 
  67.      * 第二种:不抛异常姿势。 
  68.      * (若只是简单循环集合,可用for each循环,若要增删集合,则用for i 循环,避免出错) 
  69.      */  
  70.     private static void rightWay1(List<String> list) {  
  71.         for (int i = 0; i < list.size(); i++) {  
  72.             if ("5".equals(list.get(i))) {  
  73.                 list.remove(i);  
  74.             }  
  75.         }  
  76.     }  
  77.   
  78.     /** 
  79.      * 第三种:会抛ConcurrentModificationException的异常方法 
  80.      */  
  81.     private static void wrongWay2(final List<String> list) {  
  82.         new Thread(new Runnable() {  
  83.             @Override  
  84.             public void run() {  
  85.                 Iterator<String> iterator = list.iterator();  
  86.                 while (iterator.hasNext()) {  
  87.                     System.out.println(iterator.next());  
  88.                     try {  
  89.                         Thread.sleep(10);  
  90.                     } catch (InterruptedException e) {  
  91.                         e.printStackTrace();  
  92.                     }  
  93.                 }  
  94.             }  
  95.         }).start();  
  96.         new Thread(new Runnable() {  
  97.             @Override  
  98.             public void run() {  
  99.                 for (int i = 0; i < list.size(); i++) {  
  100.                     if (list.get(i).equals("4")) {  
  101.                         list.remove(i);  
  102.                     }  
  103.                 }  
  104.             }  
  105.         }).start();  
  106.     }  
  107.   
  108. }  


具体解释,前2种是每次都必会抛异常的,但是第三种,多线程的并发操作的时候,可能会成功,不是百分百抛异常。多测试几下才会抛异常。多线程并发的测试结果图我就上一下吧。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值