简体   繁体   English

Guava Multimap .values()集合会引发并发修改异常吗?

[英]Will the Guava Multimap .values() collection throw concurrent modification exceptions?

will the Guava Multimap .values() collection throw concurrent modification exceptions if the return collection if iterated on another thread while the main multimap is changed on another thread? 如果return集合在另一个线程上进行迭代而在另一个线程上更改主multimap时返回的集合,Guava Multimap .values()集合是否会引发并发修改异常?

If so how can this is avoided? 如果是这样,如何避免呢?

Basically I need to return the Multimap .values() collection from a method running on another thread while the main thread may update the Multimap. 基本上,我需要从在另一个线程上运行的方法返回Multimap .values()集合,而主线程可能会更新Multimap。

Does this statement from the docs mean I'll be ok? 文档中的这句话是否意味着我会没事的? " * *

This class is not threadsafe when any concurrent operations update the * multimap. 当任何并发操作更新* multimap时,此类不是线程安全的。 Concurrent read operations will work correctly. 并发读取操作将正常运行。 To allow concurrent * update operations, wrap your multimap with a call to {@link * Multimaps#synchronizedListMultimap}." 要允许并发*更新操作,请通过调用{@link * Multimaps#synchronizedListMultimap}包装多图。”

Thanks 谢谢

Short answer: yes it does throw ConcurrentModificationException; 简短的答案:是的,它的确抛出了ConcurrentModificationException; I was able to get one using: 我能够使用以下方法之一:

 Multimaps.synchronizedListMultimap(ArrayListMultimap.<Integer, Integer>create());

The iterator that is return for Multimap.values() is a proxy(composite iterator) for HashMap and ArrayList 返回的Multimap.values()迭代器是HashMap和ArrayList的代理(复合迭代器)

As a quick hacky ugly fix you can just shyncronize on that multimap. 作为一个棘手的丑陋修补程序,您可以仅在该多地图上进行同步。 This works because Multimaps.synchronizedListMultimap returns a SynchronizedListMultimap. 这是可行的,因为Multimaps.synchronizedListMultimap返回了SynchronizedListMultimap。 SynchronizedListMultimap extends SynchronizedObject and this one is build with a null mutex so it will use itself for syncronization. SynchronizedListMultimap扩展了SynchronizedObject,并且此模型是使用空互斥锁构建的,因此它将用于同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM