Collection ConcurrentModificationException异常

/** * ConcurrentModificationException异常,此异常通常在一个集合为多个线程使用的场景中。 * <p><strong>解决办法:</strong></p> * <p> * 1、使用copy办法,如<pre> * String[] tmpStrArr = new String[arrayListInst.size()]; * for (String tmp : tmpStrArr) { * System.out.println(tmp); * } * </pre> * 2、使用特定的集合实例,需要花费性能代价 * </p> * @author WangYanCheng * @version 2010-11-10 */ public class ConcurrentExceptionArrayListTest { /** 集合 */ private List<String> rtnList = /*new ArrayList<String>();*/new CopyOnWriteArrayList<String>();/*Collections.synchronizedList(new ArrayList<String>());*/ /** * 数据生产者 * @author WangYanCheng * @version 2011-6-16 */ class ProducterThread extends Thread { private boolean startFlag = true; private Random randInst; /** * Constructor */ public ProducterThread() { randInst = new Random(); } /** * 线程停止 * @param startFlag */ public void stopThread(boolean startFlag) { this.startFlag = startFlag; } /** * runnable */ public void run() { while (this.startFlag) { rtnList.add(String.valueOf(randInst.nextDouble())); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } /** * 数据消费者 * @author WangYanCheng * @version 2011-6-16 */ class ConsumerThread extends Thread { private boolean startFlag = true; /** * 线程停止 * @param startFlag */ public void stopThread(boolean startFlag) { this.startFlag = startFlag; } public void run() { while (this.startFlag) { doPrintList(rtnList.toArray(new String[rtnList.size()])); doPrintList(rtnList.iterator()); try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } } } } public void doPrintList(Iterator<String> iterator) { for (;iterator.hasNext();) { System.out.print(iterator.next() + "/t"); } System.out.println(); } public void doPrintList(String[] listArr) { for (String str : listArr) { System.out.print(str.concat("/t")); } System.out.println(); } /** * 生成消费者线程对象 * @return {@link ConsumerThread} */ public ConsumerThread getConsumerThread() { return new ConsumerThread(); } /** * 生成生产者线程对象 * @return {@link ProducterThread} */ public ProducterThread getProducterThread() { return new ProducterThread(); } /** * 测试入口 * @param args 参数列表 */ public static void main(String[] args) { ConcurrentExceptionArrayListTest cowALT = new ConcurrentExceptionArrayListTest(); cowALT.getProducterThread().start(); cowALT.getConsumerThread().start(); try { Thread.sleep(500000); } catch (InterruptedException e) { e.printStackTrace(); } } }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值