简体   繁体   English

ConcurrentSkipListSet 如何具有弱一致性的迭代器? 理解“弱一致性”的含义

[英]How does ConcurrentSkipListSet has Iterators that are weakly consistent? Understanding meaning of 'weakly consistent'

Fail-fast iterator iterates over collection.快速失败迭代器迭代集合。 If collection gets modified while iterating, we get exception.如果集合在迭代时被修改,我们会得到异常。 Opposite applies for fail-safe, where iteration is happening on one collection, while write operation happen on copy of it, thus it is how fail-safe works (fe CopyOnWriteArrayList).相反适用于故障安全,其中迭代发生在一个集合上,而写操作发生在它的副本上,因此故障安全是如何工作的(fe CopyOnWriteArrayList)。

Can someone explain me how does ConcurrentSkipListSet has fail-safe?有人可以解释一下 ConcurrentSkipListSet 是如何实现故障安全的吗? There are no copies when modifying collection (like CopyOnWrite classes do), so how does it happen?修改集合时没有副本(就像 CopyOnWrite 类一样),那么它是如何发生的呢? I read because its Iterator is weakly consistent.我读是因为它的迭代器是弱一致的。 I read docs, I still don't understand.我读了文档,我还是不明白。 (but I do know what code visibility or happens-before relation in concurrency is). (但我确实知道并发中的代码可见性或发生之前的关系是什么)。

Does anyone have logic and easy to remember explanation, as I am a beginner?有没有人有逻辑和容易记住的解释,因为我是初学者?

//Example: //例子:

 ConcurrentSkipListSet<Integer> set = new ConcurrentSkipListSet<>();
      set.add(1);
      set.add(2);
      set.add(3);
      set.add(4);

      Iterator<Integer> iterator = set.iterator();
      while (iterator.hasNext()){
          System.out.println(iterator.next());
          set.remove(4);
      }

OUTPUT:
1
2
3

I was expecting ConcurrentException to be thrown here.. Please help:(我期待在这里抛出 ConcurrentException .. 请帮助:(

The "weakly consistent" term is defined in the java.util.concurrent package description : “弱一致性”术语在java.util.concurrent package 描述中定义:

Most concurrent Collection implementations (including most Queues) also differ from the usual java.util conventions in that their Iterators and Spliterators provide weakly consistent rather than fast-fail traversal:大多数并发 Collection 实现(包括大多数队列)也不同于通常的 java.util 约定,因为它们的 Iterators 和 Spliterators 提供弱一致性而不是快速失败遍历:

  • they may proceed concurrently with other operations他们可以与其他操作同时进行
  • they will never throw ConcurrentModificationException他们永远不会抛出ConcurrentModificationException
  • they are guaranteed to traverse elements as they existed upon construction exactly once, and may (but are not guaranteed to) reflect any modifications subsequent to construction.它们保证在构造时只遍历元素一次,并且可能(但不保证)反映构造后的任何修改。

In this case with ConcurrentSkipListSet , the iterator does not have a "fast-fail" property, instead it reflects the modification of 4 having been removed from the set.在使用ConcurrentSkipListSet的情况下,迭代器没有“快速失败”属性,而是反映4的修改已从集合中删除。

Internally, ConcurrentSkipListSet is implemented with ConcurrentSkipListMap , and its iterators are implemented by keeping track of which skip list node should be traversed next.在内部, ConcurrentSkipListSet是用ConcurrentSkipListMap实现的,它的迭代器是通过跟踪接下来应该遍历哪个跳过列表节点来实现的。 This naturally gives you the "weakly consistent" property: If the next item is deleted, the iterator will still return it.这自然会为您提供“弱一致性”属性:如果删除了下一项,迭代器仍会返回它。 If items beyond the next are deleted, the iterator will reflect those changes.如果删除了下一个之后的项目,迭代器将反映这些更改。

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

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