商务合作:179001057@qq.com

java.util.ConcurrentModificationException 异常

技术2022-05-11  0


某平台价值19860元的编程课程资料免费领取【点我领取】


使用 jbpm时,查找需要singal的task时代码如下, 

    Iterator it1=collect.iterator();   while(it1.hasNext()){     TaskInstance taskInstance=(TaskInstance)it1.next(); //在该行处报异常。     Set actorSet = taskInstance.getPooledActors();     Iterator iterator = actorSet.iterator();     while(iterator.hasNext()){      PooledActor poolAct = (PooledActor)iterator.next();      if((""+userId).equals(poolAct.getActorId())       &&!taskInstance.hasEnded() ){        Set set = taskInstance.getToken().getNode().getArrivingTransitions();       taskInstance.end();       }     }    }

在网上查找到,

一个Collection进行遍历(iteration)同时,对这个Collection里面的元素(element)进行做修改时,就会抛出 java. util. ConcurrentModificationException. 故将代码修改为如下: //Iterator it1=collect.iterator(); //修改1     Object[] objs = collect.toArray();     for(int j=0;j<objs.length;j++){ //    while(it1.hasNext()){ //修改2      TaskInstance taskInstance=(TaskInstance)objs[j];      Set actorSet = taskInstance.getPooledActors();      Iterator iterator = actorSet.iterator();      while(iterator.hasNext()){       PooledActor poolAct = (PooledActor)iterator.next();       if((""+userId).equals(poolAct.getActorId())        &&!taskInstance.hasEnded() ){        Set set = taskInstance.getToken().getNode().getArrivingTransitions();        taskInstance.end();       }      }     } 经测试通过。

最新回复(0)