ConcurrentModificationException when you concurrent modify the collection for its interating
来源:互联网 发布:华南师范网络教育考题 编辑:程序博客网 时间:2024/05/16 05:40
Today, I was in a trouble when try to change the collection(vector or list) for a collection loop/interator. It throws ConcurrentModificationException. Thereby, I have to look up the java API.
int java.util.AbstractList.modCount
/**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
The number of times this list has been structurally modified. Structural modifications are those that change the size of the list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.
This field is used by the iterator and list iterator implementation returned by the iterator and listIterator methods. If the value of this field changes unexpectedly, the iterator (or list iterator) will throw a ConcurrentModificationException in response to the next, remove, previous, set or add operations. This provides fail-fast behavior, rather than non-deterministic behavior in the face of concurrent modification during iteration.
Use of this field by subclasses is optional. If a subclass wishes to provide fail-fast iterators (and list iterators), then it merely has to increment this field in its add(int, E) and remove(int) methods (and any other methods that it overrides that result in structural modifications to the list). A single call to add(int, E) or remove(int) must add no more than one to this field, or the iterators (and list iterators) will throw bogus ConcurrentModificationExceptions. If an implementation does not wish to provide fail-fast iterators, this field may be ignored.
java.util.ConcurrentModificationException
This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.
For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.
Note that fail-fast behavior cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast operations throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: ConcurrentModificationException should be used only to detect bugs.
- ConcurrentModificationException when you concurrent modify the collection for its interating
- Modify the average program to promote for intergers repeatedly.stop when a nagetive number is entere
- Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop
- It will cause a new session when you use lazy loading collection, and the collection will assicated with the new session and the
- Java concurrent exception in the synchronized collection
- Modify the PAMIE for attach opened IE
- When you should use base64 for images
- The Cranberries-When you'r gone
- when you're gone----the cranberries 小红莓
- 393. The cat shuts its eyes when stealing. 掩耳盗铃
- 393. The cat shut its eyes when stealing. 掩耳盗铃
- Typically this problem occurs when you are putting in a data that is too long for the column. In t
- Collection ConcurrentModificationException异常
- Choosing the Right API for Concurrent Programming
- When you...
- modify is ambiguous in Mapped Statements collection (try using the full name including the namespace
- Modify the /etc/apt/sources.list for Ubuntu
- Runescape Cheats Is Great For When You’re Eating
- VS2008编译的程序在某些机器上运行提示“由于应用程序配置不正确,应用程序...
- 博客是需要用心经营的
- linux sed 批量替换字符串
- 如何获得一个COM接口的事件源的GUID
- 浮点数的精度和转换(转)
- ConcurrentModificationException when you concurrent modify the collection for its interating
- Java编程之System
- RTEMS 的 AT91SAM9260 移植(6): 杂项函数
- 增强沟通技巧和提高开发效率
- 聊天
- 关于锁的一些常见问题
- 如何强化 TCP/IP 堆栈安全
- 我搜集改版上线 http://www.06w03.tk
- 如何禁用shell中的beep声音