3

我们正在创建一个由 JADE Agent 结构中的 AID 组成的树形结构。我们选择递归执行,这样无论系统中当前有多少代理程序,代码都可以执行,树结构可以动态更改以适应系统中当前代理程序的数量。我不确定是否需要设置某种锁以确保Map读取和写入对象而不破坏结构的完整性。

这是导致问题的代码。

// BuildHierarchy method used to create the hierarchy based on Monitor Agents in the system
private void BuildHierarchy(Map<AID, Double>freqList, ArrayList<AID> childless, DefaultMutableTreeNode node){
    int i = 0;
    //creates an iterator for the freqList
    Iterator iter = freqList.entrySet().iterator();

    while(iter.hasNext()&& i < 2){

        Map.Entry pairs = (Map.Entry)iter.next();   
        //if (i<2){ 
            setParentNode((AID)pairs.getKey(), node);
        //}
        freqList.remove(pairs.getKey());
        i++;
    }
    BuildHierarchy(freqList, childless, node.getNextNode());
    BuildHierarchy(freqList, childless, node.getNextNode().getNextSibling());

}
4

2 回答 2

2

在迭代它时,您不能修改Set(或底层),除非通过迭代器自己的操作,否则将抛出 a。尝试MapremoveConcurrentModificationException

iter.remove();

代替

freqList.remove(pairs.getKey());
于 2013-05-24T19:21:52.023 回答
0

当在已实现的集合上创建迭代器时,会在其上创建一个支持集,并且有一个计数器用于跟踪已实现的集合大小。在迭代期间,如果集合被修改,请说“freqList.remove(pairs.getKey());” 然后它将删除元素并减小集合的大小,现在当下次迭代器对其调用 next() 操作时,它会从计数器实例中检测到集合已被修改并抛出 ConcurrentModificationException 。HashIterator 类的以下代码将使您清楚地了解它是如何工作的

     final Entry<K,V> nextEntry() {
      if (modCount != expectedModCount)
             throw new ConcurrentModificationException();
         Entry<K,V> e = next;
         if (e == null)
            throw new NoSuchElementException();

         if ((next = e.next) == null) {
            Entry[] t = table;
            while (index < t.length && (next = t[index++]) == null)
                 ;
         }
        current = e;
        return e;
     }

如果使用实际集合删除条目,则 modcount !=expectedCount 将为 true 并抛出异常。

于 2013-05-25T08:35:06.647 回答