弱一致的迭代器

    java.util 包中的集合类都返回 fail-fast 迭代器,这意味着它们假设线程在集合内容中进行迭代时,集合不会更改它的内容。如果 fail-fast 迭代器检测到在迭代过程中进行了更改操作,那么它会抛出 ConcurrentModificationException,这是不可控异常。

在迭代过程中不更改集合的要求通常会对许多并发应用程序造成不便。相反,比较好的是它允许并发修改并确保迭代器只要进行合理操作,就可以提供集合的一致视图,如 java.util.concurrent 集合类中的迭代器所做的那样。

    java.util.concurrent 集合返回的迭代器称为弱一致的(weakly consistent)迭代器。对于这些类,如果元素自从迭代开始已经删除,且尚未由 next() 方法返回,那么它将不返回到调用者。如果元素自迭代开始已经添加,那么它可能返回调用者,也可能不返回。在一次迭代中,无论如何更改底层集合,元素不会被返回两次。




Weak consistency

The name weak consistency may be used in two senses. In the first sense, strict and more popular, the weak consistency is one of theconsistency models used in the domain of the concurrent programming (e.g. in distributed shared memory, distributed transactions etc.).

The protocol is said to support weak consistency if:

  1. All accesses to synchronization variables are seen by all processes (or nodes, processors) in the same order (sequentially) - these are synchronization operations. Accesses to critical sections are seen sequentially.
  2. All other accesses may be seen in different order on different processes (or nodes, processors).
  3. The set of both read and write operations in between different synchronization operations is the same in each process.

Therefore, there can be no access to a synchronization variable if there are pending write operations. And there can not be any new read/write operation started if system is performing any synchronization operation.

In the second, more general, sense weak consistency may be applied to any consistency model weaker than sequential consistency.

The opposite of weak consistency is strong consistency, where parallel processes can observe only one consistent state.


http://en.wikipedia.org/wiki/Weak_consistency





Strong consistency

Strong consistency is one of the consistency models used in the domain of the concurrent programming (e.g. in distributed shared memory,distributed transactions etc.).

The protocol is said to support strong consistency if:

  1. All accesses are seen by all parallel processes (or nodes, processors etc.) in the same order (sequentially)

Therefore only one consistent state can be observed, as opposed to weak consistency, where different parallel processes (or nodes etc.) can perceive variables in different states.


http://en.wikipedia.org/wiki/Strong_consistency







你可能感兴趣的:(java,迭代器)