Difference between ArrayList and CopyOnWriteArrayList
There are four concrete implementation of List interface:-
- Vector
- ArrayList
- LinkedList
- CopyOnWriteArrayList
Different between Vector,ArrayList and LinkedList is quite clear but
difference between ArrayList and CopyOnWriteArrayList doesn’t seems to
be clear at first glance. Here I am trying to explain between difference
between ArrayList and CopyOnWriteArrayList , with one simple
example.As we all know ArrayList is not thread safe so any simultaneous
thread can access and modify the content of list simultaneously.Here
lies the dangerous, ConcurrentModificationException. When one thread is
Iterating over an ArrayList and any other thread(which may be the same
thread) modify the content of list and when we again call next() on the
iterator ,we get this exception. Which means that no thread can modify
the ArrayList while an Iterator is iterating over this. We can solve
this by surrounding the code that try to modify this list in a
synchronized block with some lock so that the thread trying to modify
the ArrayList wait for the iterator to complete.
This seems simple solution but not a efficient one where there are
many threads iteration over an ArrayList because each thread have to
wait for a considerable time.
Another possible solution could be to use CopyOnWriteArrayList
instead of ArrayList. This is same as ArrayList with one difference. All operations that change the contents of a
CopyOnWriteArrayList
collection cause the underlying array to be replaced with a copy of
itself before the contents of the array are changed. Any active
iterators will continue to see the unmodified array, so there is no need
for locks. Iterators created by a
CopyOnWriteArrayList
object cannot change the underlying array. Though these iterators do
have methods for changing the underlying collection, their
implementations throw an
UnsupportedOperationException
instead of modifying the underlying collection.
Lets consider first case when we modify an ArrayList while an Iterator is traversing it:-
import java.util.*;
import java.util.concurrent.*;
import java.math.*;
class test{
public final static void main(String args[]){
List list= new ArrayList();
list.add("vivek");
list.add("kumar");
Iterator i =list.iterator();
while(i.hasNext()){
System.out.println(i.next());
list.add("abhishek");
}
}
}
Output:-
vivek
Exception in thread “main”
java.util.ConcurrentModificationExceptionat
java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
at java.util.AbstractList$Itr.next(AbstractList.java:420)
at test.main(test.java:15)
Explanation:
As we have modified the ArrayList
after displaying first element, it will throw a
ConcurrentModificationException at when we call next() next time.
Now see the same program with CopyOnWriteArrayList:-
import java.util.*;
import java.util.concurrent.*;
import java.math.*;
class test{
public final static void main(String args[]){
CopyOnWriteArrayList list = new CopyOnWriteArrayList();
list.add("vivek");
list.add("kumar");
Iterator i =list.iterator();
while(i.hasNext()){
System.out.println(i.next());
list.add("abhishek");
}
System.out.println("After modification:");
Iterator i2 =list.iterator();
while(i2.hasNext()){
System.out.println(i2.next());
}
}
}
Output:-
E:\>java test
vivek
kumar
After modification:
vivek
kumar
abhishek
abhishek
vivek
kumar
After modification:
vivek
kumar
abhishek
abhishek
Explanation:-
We did not get any ConcurrentModificationExceptionat as CopyOnWriteArrayList
keeps a copy of original List and iterate over this. The new value
which has been added is merged to copy of orignal array only after
Iteration is over.
We can not use iterator of CopyOnWriteArrayList to modify the List.
Lest consider a normal ArrayList and try to modify the list using iterator.
import java.util.*;
import java.util.concurrent.*;
import java.math.*;
class test{
public final static void main(String args[]){
ArrayList list = new ArrayList();
list.add("vivek");
list.add("kumar");
Iterator i =list.iterator();
while(i.hasNext()){
System.out.println(i.next());
i.remove();
}
System.out.println("After modification:");
Iterator i2 =list.iterator();
while(i2.hasNext()){
System.out.println(i2.next());;
}
}
}
Output:-
vivek
kumar
After modification:
Explanation:-
we can use iterator’s method (remove) to modify the list. Now check for CopyOnWriteArrayList
import java.util.*;
import java.util.concurrent.*;
import java.math.*;
class test{
public final static void main(String args[]){
CopyOnWriteArrayList list = new CopyOnWriteArrayList();
list.add("vivek");
list.add("kumar");
Iterator i =list.iterator();
int j=0;
while(i.hasNext()){
System.out.println(i.next());
list.add("abhishek");
i.remove();
}
}
}
Output:-
vivek
Exception in thread “main” java.lang.UnsupportedOperationException
at java.util.concurrent.CopyOnWriteArrayList$COWIterator.remove(CopyOnWr
iteArrayList.java:937)
at test.main(test.java:17)
Explanation:-
we cannot use iterator method to modify the elements of CopyOnWriteArrayList
分享到:
相关推荐
difference between ArrayList and LinkedList?` LinkedList and ArrayList are two different implementations of the List interface. LinkedList implements it with a doubly-linked list. ArrayList implements...
AttributeList , ArrayList , CopyOnWriteArrayList , LinkedList RoleList , RoleUnresolvedList , Stack Vector Set接口 ConcurrentSkipListSet , CopyOnWriteArraySet , EnumSet…, HashSet JobStateReasons , ...
集合ArrayList测试集合ArrayList测试集合ArrayList测试集合ArrayList测试集合ArrayList测试集合ArrayList测试
ArrayList是一种以数组实现的List,它实现了List, RandomAccess, Cloneable, Serializable接口。 实现List接口表示它可以支持删除、添加和查找等操作。 实现RandomAccess接口表示它可以支持随机访问(强调一点,并不...
一个C++(Ubuntu16.04+QT5.9.1)通过JNI,调用JAVA类及方法的示例。通过JNI传递和返回多种类型的参数,boolean ,int,String,ArrayList,ArrayList嵌套ArrayList<ArrayList<String>>等。
在jni中操作arraylist对象,然后添加一个int型数据进去
arrayList排序arrayList排序arrayList排序
// Adds and item and returns added item. Array is resized by one. pItemType Add(pItemType anItem); // Removes the item at the given index. Array is resize by one. void RemoveItemAtIndex(const int...
使用对象ArrayList填充DataGrid,C#源代码ArrayList MyList = new ArrayList();
ArrayList常用的方法如下: Add 添加到 ArrayList 的結尾處 Remove 從 ArrayList 中移除特定對象的第一個匹配項 RemoveAt 移除 ArrayList 的指定索引處的元素 Insert 將元素插入 ArrayList 的指定索引處 ...
ASP .NET - ArrayList对象ASP .NET - ArrayList对象ASP .NET - ArrayList对象ASP .NET - ArrayList对象
ArrayList转化为DataTable数据加载转换方便程序的灵活运用!
arraylist .
C语言版的ArrayList,具有ArrayList的基本方法增加、插入、删除、自动扩容等。
什么是ArrayList? 如何使用ArrayList? ArrayList重要的方法和属性?
C# Array和ArrayList,List区别
浅析ArrayList内部实现 资源源于不但搜索,自由源于不但努力
java中ArrayList的用法
在Java中,CopyOnWriteArrayList(写入时复制数组列表)是线程安全的集合类,它实现了List接口,并使用了"写入时复制"的...这样,在并发修改时,可以避免读取和写入同时进行,从而避免了传统ArrayList的线程同步问题。
java.util包中的ArrayList很常用,参考jdk源码中的ArrayList.java,写了一个c版的ArrayList,目前仅坚持 char *(字符串) 和 int (整型).