gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:25:07 25 4
gpt4 key购买 nike

试图解决这个问题 2 周,但没有任何成功。 :X

当我进行任何类型的迭代时都会发生这种情况,但主要是在使用#forEach 时。

我没有以任何方式修改列表,也没有修改它的元素,所以这对我来说似乎很尴尬。示例代码:

    Map<Season, List<Team>> map = fetcher.getTeamsIn(ids);

Set<Team> toCreateTeams = new HashSet<>();
Set<Team> toUpdateTeams = new HashSet<>();

map.forEach((k, v) -> {
toCreateTeams.addAll(v.stream().filter(t -> !persistedTeams.containsKey(t.getId())).collect(Collectors.toSet()));
toUpdateTeams.addAll(v.stream().filter(t -> {
Date latestPersistedUpdate = persistedTeams.get(t.getId());
return latestPersistedUpdate != null && t.getLastUpdated().after(latestPersistedUpdate);
}).collect(Collectors.toSet()));
});

map 在 #getTeamsIn 中实例化为 new HashMap<>();

试图在 eclipse 中中断异常,看看是否有线程在做一些疯狂的事情,但对我来说一切似乎都很正常。在下面的图片中,在迭代 map 时抛出了异常。 .

ConcurrentModificationException thread stack trace ConcurrentModificationException thread stack trace

我也开始出现其他一些非常奇怪的行为,比如永远卡在 lambda 表达式中。在这种情况下,在我看来 Eclipse 停止在表达式中(出于某种未知原因),就好像在该行中设置了某个断点一样。当我暂停执行并恢复有问题的线程时,流程恢复正常(直到下一个 lambda 表达式)或一些疯狂的 ConcurrentModificationException。

stopping in expression for unknown reason

对我来说,整个事情就像是 Eclipse 的一些疯狂错误,但如果是这样的话,我真的不想重建我的环境。

我在用

Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

在 Linux Mint 上。

谢谢!

-- 更新 1 --

需要说明的是:即使是这个简单的例子也会发生错误:

map.forEach((k, v) -> {
System.out.println("Test" + k.getId());
});

一些可能很重要的随机信息:异常仅在打印 map 的最后一个元素后爆炸!

-- 更新 2 --

关于更新 1 中的随机信息,这真的不重要,因为出于性能原因(至少在 HashMap 和 ArrayList 中),ConcurrentModificationException仅在迭代结束时通过将元素数组的实际大小与其预期大小进行比较来检查。

#getTeamsIn 方法的代码:

public Map<Season, List<Team>> getTeamsIn(List<Season> seasons) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(seasons.size());
Map<Season, List<Team>> teamsInSeason = new HashMap<>();
for (Season s : seasons) {
httpclient.execute(new HttpGet(String.format(URL, s.getId())),
new Callback(latch) {

@Override
public void completed(final HttpResponse response) {
super.completed(response);
try {
teamsInSeason.put(s, new TeamsUnmarshaller().unmarshal(response.getEntity().getContent()));
}
catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
System.out.println(e);
}
}

});
}
latch.await();
return teamsInSeason;
}

Callback类只是implements FutureCallback<HttpResponse>countDown() latch在所有回调方法中(#cancelled、#completed 和#failed)。

最佳答案

好的,刚发现问题。我在方法#getTeamsIn 中重写的#completed 方法返回的时间太长(感谢 JAXB)。由于 countDown()(在 super.completed(response) 中调用)在 teamsInSeason.put(s, new TeamsUnmarshaller().unmarshal(response) 之前.getEntity().getContent()));,我们遇到问题了。

修复简单而丑陋:

@Override
public void completed(final HttpResponse response) {
try {
teamsInSeason.put(s, new TeamsUnmarshaller().unmarshal(response.getEntity().getContent()));
}
catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
System.out.println(e);
} finally {
super.completed(response);
}
}

奇怪的 Eclipse 行为(由于某种未知原因卡在某个假想的断点),如果它持续存在,我认为是另一个主题的问题。

感谢大家的帮助和时间!

关于进行任何类型的迭代时出现 Java 8 ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29909286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com