Concurrent modification error when adding elements to LinkedList

后端 未结 3 1803
长情又很酷
长情又很酷 2021-01-24 01:09

I have an List of LinkedList objects.

    List> backup = new ArrayList>();

The Link

相关标签:
3条回答
  • 2021-01-24 01:14

    It's probably because you're editing the list, then trying to use the original iterators. The collections API doesn't allow that. You need to create new iterators after editing the list.

    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.

    0 讨论(0)
  • 2021-01-24 01:15

    First, if you really expect people to give their attention to your question, you should do them the favor of asking it clearly and in standard English.

    Second, you should provide an indication of exactly where in your code you're getting the ConcurrentModificationError.

    Finally, what is it_bk? It just shows up in your code without any explanation. If it is an ArrayList of ListIterators it is certainly possible that its parent_node-th element is in a state where it isn't sure that hasNext() or next() will be safe. I'm guessing you modified the underlying collection with your tmp_bk.add(f_new); and so a pre-existing iterator is worried that its invariants may be violated.

    General advice: Don't create and retain iterators (or collections of them). When you want an Iterator, create it, use it, and abandon it.

    0 讨论(0)
  • 2021-01-24 01:33

    java.lang.Colletions from JDK 1.5 is not synchronized. In earlier version(jdk 1.4), you wont find this problem.

    There are multiple solutions available for these problem and you need to choose one of them wisely as per your use case.

    • Solution 1: The list can be converted to an array with list.toArray() and iterate on the array. This approach is not recommended if the list is large.

    • Answer 2: The entire list can be locked while iterating by wrapping your code within a synchronized block. This approach adversely affects scalability of your application if it is highly concurrent.

    • Answer 3: JDK 1.5 gives you ConcurrentHashMap and CopyOnWriteArrayList classes, which provide much better scalability and the iterator returned by ConcurrentHashMap.iterator() will not throw ConcurrentModificationException while preserving thread-safety.

    • Answer 4: Remove the current object via the Iterator “it” which has a reference to the underlying collection “myStr”. The Iterator object provides it.remove() method for this purpose.

    0 讨论(0)
提交回复
热议问题