日期:2014-05-20  浏览次数:20702 次

急!!!!java.util.ConcurrentModificationException异常问题
我在一个timer的run中调用了一个方法,获取一个collection,但同时还有其他的地方调用了相同的方法,获取collection,程序启动时,当collection中的值大于等于2时,遍历collection时,遍历到第二个值时,就会报java.util.ConcurrentModificationException异常,请问有什么办法可以解决吗?
  将collection强制转换为list不行,强制转换后都不会进入判断循环遍历了,给被调用的方法getAllStoroageSystem()加了同步也不行,还是会报这个异常
  各位高手们,请指教下啊
  Collection storageSystemList = StorageSystemManager.getAllStoroageSystem();

System.out.println("ccc" + storageSystemList.size());

if(storageSystemList != null && storageSystemList.size() > 0){

for(Iterator iter = storageSystemList.iterator(); iter.hasNext();){

StorageSystem storageSystem = (StorageSystem) iter.next();

String sysName = storageSystem.getName();
  }

------解决方案--------------------
不会有性能问题,放心吧,没有了锁,引用的存储,实际上在多对象的时候还会更快

Java code
        List list=new ArrayList();
        for (Iterator iter = storageSystemList.iterator(); iter.hasNext();) {
            list.add(iter.next());
        }
        for(int i=0;i<list.size();i++){
            StorageSystem storageSystem = (StorageSystem)list.get(i);
// 下面接String sysName = storageSystem.getName();那一行