检测并发修改? [英] Detecting concurrent modifications?

查看:104
本文介绍了检测并发修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的多线程应用程序中,我们偶尔会在我们的列表中看到 ConcurrentModificationExceptions (主要是 ArrayList ,有时Vectors)。但有些时候,我认为并发修改发生,因为迭代通过集合似乎是缺少的项目,但没有异常抛出。我知道 ConcurrentModificationException 的文档说你不能依赖它,但我如何去确保我不是同时修改列表?

更新:是的,我知道 Collections.synchronizedCollection ,但它不防止某人在迭代过程中修改集合。



第二次更新如果您在第二次更新时遇到问题,有人想要提到的synchronizedCollection和克隆像Jason做的,提到java.util.concurrent和像jacekfoo和Javamann这样的apache集合框架,我可以接受一个答案。


<你原来的问题似乎是要求一个迭代器看到基础集合的实时更新,同时保持线程安全。这是一个非常昂贵的问题,在一般情况下解决,这就是为什么没有一个标准的集合类做它。



有很多方法实现部分解决方案



Jason提供实现线程安全的特定方式,避免引发ConcurrentModificationException ,但只会牺牲活性。



Javamann在 java.util.concurrent 中提及两个特定类, code> package以无锁方式解决同样的问题,其中可扩展性至关重要。这些只是Java 5附带的,但已经有各种项目,将软件包的功能退回到早期的Java版本,包括这一个,虽然他们在早期的JRE中不会有这么好的性能。



如果你已经使用了一些Apache Commons库, jacekfoo 指出

您还可以考虑查看 Google collections框架


In a multi-threaded application I'm working on, we occasionally see ConcurrentModificationExceptions on our Lists (which are mostly ArrayList, sometimes Vectors). But there are other times when I think concurrent modifications are happening because iterating through the collection appears to be missing items, but no exceptions are thrown. I know that the docs for ConcurrentModificationException says you can't rely on it, but how would I go about making sure I'm not concurrently modifying a List? And is wrapping every access to the collection in a synchronized block the only way to prevent it?

Update: Yes, I know about Collections.synchronizedCollection, but it doesn't guard against somebody modifying the collection while you're iterating through it. I think at least some of my problem is happening when somebody adds something to a collection while I'm iterating through it.

Second Update If somebody wants to combine the mention of the synchronizedCollection and cloning like Jason did with a mention of the java.util.concurrent and the apache collections frameworks like jacekfoo and Javamann did, I can accept an answer.

解决方案

Your original question seems to be asking for an iterator that sees live updates to the underlying collection while remaining thread-safe. This is an incredibly expensive problem to solve in the general case, which is why none of the standard collection classes do it.

There are lots of ways of achieving partial solutions to the problem, and in your application, one of those may be sufficient.

Jason gives a specific way to achieve thread safety, and to avoid throwing a ConcurrentModificationException, but only at the expense of liveness.

Javamann mentions two specific classes in the java.util.concurrent package that solve the same problem in a lock-free way, where scalability is critical. These only shipped with Java 5, but there have been various projects that backport the functionality of the package into earlier Java versions, including this one, though they won't have such good performance in earlier JREs.

If you are already using some of the Apache Commons libraries, then as jacekfoo points out, the apache collections framework contains some helpful classes.

You might also consider looking at the Google collections framework.

这篇关于检测并发修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆