Problems caused by simultaneous modification when traversing the ArrayList.

Source: Internet
Author: User

Problems caused by simultaneous modification when traversing the ArrayList.

I saw a blog, but it was not completely written. So I added the following:

Blog: http://www.cnblogs.com/alipayhutu/archive/2012/08/11/2634073.html

Note: I added the yellow font

Why does CopyOnWriteArrayList exist?

A thread-safe variant of ArrayList, all its variable operations (Add,SetIs implemented by performing a new copy on the underlying array, which is expensive.

CopyOnWriteArrayList is generated due to "concurrency.

 

Scenario 1: An error is returned when ArrayList is directly traversed and deleted.

// Delete/modify an element
 for(String item : list){
     list.remove(item);
 }

 

 

Cause: the foreach syntax is implemented through Iterator. When this List is traversed, an ArrayList is generated. itr object. This private internal class implements the Iterator interface, that is, the above Code has the same effect as the following code:

Iterator it = list. iterator ();

While (it. hasNext ()){

String temp = it. next ();

List. remove (temp );

}

There is a variable in the ArrayList to record the number of times this ArrayList is modified. Every time you call the add/remove Method, the value of this parameter is added to one, when an Iterator object is generated, this object records the number of changes to the current ArrayList, and then calls it each time. next () checks whether the number of modifications to the current ArrayList is the same as that recorded in it. If it is different, an exception is thrown. For example, the above equivalent code is executed when it = list. iterator (); The it OBJECT records the number of changes to the list object before the code is executed. the next () statement does not have an exception, because the list has not been modified yet, but the second time it is executed. when next (), list has passed list. the remove (temp) code modifies the internal variable of the number of changes, so the number of changes to the it object record is different from the number of changes to the list, so an exception is thrown.

You only need to remember that the set cannot be modified when you use Iterator to traverse the set.

Solution 1: No error is reported when the iterator is used to traverse and delete the iterator.

// Delete/modify an element
Iterator <String> it = list. iterator ();
While (it. hasNext ()){
String ele = it. next ();
It. remove ();
}

Http://blog.sina.com.cn/s/blog_605f5b4f0100qsgf.html

When the remove Method of Iterator is used, it does not judge whether the set is changed, so the above does not show ModifyException.

Solution 2: Use CopyOnWriteArrayList to traverse and delete objects in a short time. No error is returned.

for(String item : list){ 
    list.remove(item); 
} 

When you use the remove Method of CopyOnWriteArrayList, this method will first copy the List, modify the copy, and then re-point the previous List reference to the copy, so there is no exception.

 

Scenario 2: For ArrayList, an error is reported when the iterator is used to traverse and add.

Iterator <String> it = list. iterator ();
While (it. hasNext ()){
String str = it. next ();
String tem = str + "...";
List. add (tem );
}

Cause: Because list. add modifies the variable of the number of changes in its internal storage list, the number of changes in the list record in it is different from the number of changes in the current status of the list, so an exception occurs.

Solution 1: Use CopyOnWriteArrayList instead, directly traversing and adding. No error is reported.

For (String item: list ){
String tem = item + "...";
List. add (tem );
}

 

When you use the add method of CopyOnWriteArrayList, this method will first copy the List, modify the copy, and then re-point the previous List reference to the copy, so there will be no exception,

Directly traversing is the same as using Iterator.

Solution 2: Use CopyOnWriteArrayList to traverse and add data. No error is returned.

        Iterator<String> it = list.iterator(); 
        while(it.hasNext()){ 
            String str = it.next(); 
            String tem = str + "..."; 
            list.add(tem); 
        }

 

Scenario 3: You cannot remove CopyOnWriteArrayList or iterator.

Iterator <String> it = list. iterator ();
While (it. hasNext ()){
String str = it. next ();
String tem = str + "...";
It. remove ();
}

An exception is thrown directly in the remove method implemented by CopyOnWriteArrayList, indicating that this operation is not supported. As for why this operation is still coming soon.

Solution 1: directly traverse and add/remove ()

for(String item : list){ 
    String tem = item + "..."; 
    list.remove(item); 
    list.add(tem); 
} 

The above solution author wrote an error. This code will throw a ModifyException.

[Summary]What is happening at the underlying layer? How is the CPU and memory used? To be continued...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.