jjzjj

concurrentmodification

全部标签

进行任何类型的迭代时出现 Java 8 ConcurrentModificationException

试图解决这个问题2周,但没有任何成功。:X当我进行任何类型的迭代时都会发生这种情况,但主要是在使用#forEach时。我没有以任何方式修改列表,也没有修改它的元素,所以这对我来说似乎很尴尬。示例代码:Map>map=fetcher.getTeamsIn(ids);SettoCreateTeams=newHashSet();SettoUpdateTeams=newHashSet();map.forEach((k,v)->{toCreateTeams.addAll(v.stream().filter(t->!persistedTeams.containsKey(t.getId())).co

java - 如何在并发线程中操作 "ConcurrentModificationException"和 `values()` 时避免 HashMap `put()`?

代码:我有一个哈希表privateMapmap=newHashMap();一种方法通过调用put(K,V)将K-V对放入其中。另一种方法想要从它的值中提取一组随机元素:intsize=map.size();//size>0V[]value_array=map.values().toArray(newV[size]);Randomrand=newRandom();intstart=rand.nextInt(size);intend=rand.nextInt(size);//returnvalue_array[start..end-1]这两个方法在两个不同的并发线程中被调用。错误:我遇到了

java - 在 Java 中,如何在单线程程序中抛出 ConcurrentModificationException?

这个问题在这里已经有了答案:WhyisaConcurrentModificationExceptionthrownandhowtodebugit(8个答案)关闭3年前。我正在阅读这个“FreuqentJavaconcurrencyproblems”问题,并被谈论java.util.ConcurrentModificationException的答案弄糊涂了.我对答案的理解是,这可能发生在单线程程序中。如何或什么条件导致以下代码抛出异常?Listlist=newArrayList(Arrays.asList("a","b","c"));for(Stringstring:list){lis

java - 卡在 "java.util.ConcurrentModificationException"

这是我的代码://eventListisaLinkedListpublicvoidrun(){Iteratorit=eventList.iterator();intsize=eventList.size();while(size>0){while(it.hasNext()){Evente=it.next();//flagedlineif(e.ready()){System.out.println(e);e.action();eventList.remove(e);--size;}}}}错误java.util.ConcurrentModificationException被抛出在标志线处

Java HashMap 在迭代时添加新条目

在HashMap中map=newHashMap();it=map.entrySet().iterator();while(it.hasNext()){entry=it.next();it.remove();//safelyremoveaentryentry.setValue("newvalue");//safelyupdatecurrentvalue//howtoputnewentrysetinsidethismap//map.put(s1,s2);itthrowsaconcurrentaccessexception}当我尝试添加一个新条目来映射时,它会抛出ConcurrentModi

java - LinkedList checkForComodification错误java

好的,所以我在这里尝试做的是让一个方法在给定的“时间”内“运行”一个过程,这一切都在一定程度上起作用,但它一直在给出这些错误。这是它给出的第一个异常(exception)Exceptioninthread"main"java.util.ConcurrentModificationException然后在exicutio中它给出了这个atjava.util.LinkedList$ListItr.checkForComodification(LinkedList.java:761)atjava.util.LinkedList$ListItr.next(LinkedList.java:696

Java:在修改集合内容时迭代集合

我希望迭代一个集合,但集合的内容会在迭代过程中修改。我希望在创建迭代器时迭代原始集合,而不是迭代添加到集合中的任何新元素。这怎么可能?这是set的默认行为还是我该如何实现?我能想到的一种方法是从原始集合中获取一个不会被修改的新集合,但这似乎不够优雅,必须有更好的解决方案。 最佳答案 如果您想确保看不到任何新元素,那么拍摄集合的快照对我来说就是正确的解决方案。有一些集合,例如ConcurrentSkipListSet这将允许您继续迭代,但我无法看到关于迭代器在查看新元素方面的行为的任何保证。编辑:CopyOnWriteArraySet

java - 为什么即使同步了列表也会出现ConcurrentModificationException?

我有Android多线程应用程序。两个或多个触发器可能会运行同一部分代码。我有一个对象列表。我让它被Collections.synchronizedList同步privateListmGroupItemSampleList;mGroupItemSampleList=newArrayList();mGroupItemSampleList=Collections.synchronizedList(mGroupItemSampleList);但是有时我在线上遇到异常:Collections.sort(mGroupItemSampleList,newGroupItemSampleCompara

java - 基于其中元素数量的 java.util.List 的异常行为

这个问题在这里已经有了答案:WhyisaConcurrentModificationExceptionthrownandhowtodebugit(8个答案)关闭3年前。我知道如果在某些线程使用迭代器遍历集合时更改集合,iterator.next()将抛出ConcurrentModificationException。.但它会根据列表中元素的数量显示不同的行为。我尝试了一个代码片段,其中我在for-each循环中遍历了一个列表,并在遍历之间使用列表的remove()方法从列表中删除了一个元素。理想情况下,它应该在这种情况下抛出ConcurrentModificationException

java - 集合 - Iterator.remove() 与 Collection.remove()

根据太阳,"Iterator.removeistheonlysafewaytomodifyacollectionduringiteration;thebehaviorisunspecifiediftheunderlyingcollectionismodifiedinanyotherwaywhiletheiterationisinprogress."我有两个问题:是什么让这个操作“Iterator.remove()”比其他操作更稳定?如果“Collection.remove()”方法在大多数用例中都没有用,他们为什么要提供该方法? 最佳答案