synchronizedList与会抛出ConcurrentModificationException
来源:互联网 发布:三坐标测量机编程 编辑:程序博客网 时间:2024/06/10 01:20
package synchronizedList;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class ConcurrentModificationException {public static void main(String[] args) {List<Long> list = Collections.synchronizedList(new ArrayList<Long>());for(int i=0;i<1000;i++){list.add((long) i);}Thread t = new Thread(new Delete(list));t.start();for (Long w : list) {System.out.println(w);}}}class Delete implements Runnable{private List<Long> list;public Delete(List<Long> list) {this.list = list;}@Overridepublic void run() {for (int i = 0; i < list.size(); i++) {list.remove(i);}}}
这是我自己构造的代码,拿到你的eclipse里面运行下,就会抛出java.util.ConcurrentModificationException 异常.
原因很简单, 2个线程都没有独占锁,主线程在读,子线程在修改,所以抛异常了
0 0
- synchronizedList与会抛出ConcurrentModificationException
- 抛出错误ConcurrentModificationException。
- 一个简单程序抛出ConcurrentModificationException异常
- ArrayList 使用迭代抛出ConcurrentModificationException解决方法
- ArrayList 使用迭代抛出ConcurrentModificationException解决方法
- 修改集合抛出java.util.ConcurrentModificationException
- 异步读写ArrayList换用CopyOnWriteArrayList、synchronizedList、java.util.ConcurrentModificationException 异常解决
- 遍历Map时抛出java.util.ConcurrentModificationException异常的解决办法
- 遍历Map时抛出java.util.ConcurrentModificationException异常的解决办法
- java hashMap 单线程下 抛出异常 ConcurrentModificationException
- java foreach循环中调用remove 抛出java.util.ConcurrentModificationException
- HashMap迭代时Remove不抛出ConcurrentModificationException的特例
- 集合中的Iterator抛出的ConcurrentModificationException源码分析
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- linux 下部署多个tomcat
- 关于request,session,application
- 工作中eclipse崩溃,状态栏一直显示Initializing Java Tooling: (30%)
- 用MFC画椭圆
- STM32串口发送
- synchronizedList与会抛出ConcurrentModificationException
- 2014 Gartner IaaS魔力象限市场报告解读
- Apache的Order Allow,Deny 详解
- leetcode||Single Number II
- 010回文子串最长长度
- Oracle创建临时表:CREATE GLOBAL TEMPORARY TABLE
- Srping_
- 别名现象
- IntelliJ IDEA 13 SVN问题