It is a
thread safe implementation of ArrayList. All mutative operations like add, set,
delete and so on are implemented by making a fresh copy of the underlying
array, means whenever you perform addition, setting, removal of element from CopyOnWriteArrayList,
it creates new array internally.
Since for
every mutation operation, a new array is getting created, it is very costly
operation.
Where can I use this data structure?
If you
require ArrayList which should be thread safe and require less mutation (add,
set, removal) operations and perform more traversal operations, then you can
use this data structure.
Iterator of CopyOnWriteArrayList is fail-safe?
Iterator of CopyOnWriteArrayList is fail-safe and doesn't throw ConcurrentModificationException even if underlying CopyOnWriteArrayList is modified. Once Iteration begins because Iterator is operating on separate copy of ArrayList. Consequently all the updates made on CopyOnWriteArrayList is not available to Iterator.
Iterator of CopyOnWriteArrayList is fail-safe and doesn't throw ConcurrentModificationException even if underlying CopyOnWriteArrayList is modified. Once Iteration begins because Iterator is operating on separate copy of ArrayList. Consequently all the updates made on CopyOnWriteArrayList is not available to Iterator.
import java.util.concurrent.CopyOnWriteArrayList; public class CopyOnWriteArrayListEx { public static void main(String args[]) { CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<Integer>(); list.add(1); list.add(2); System.out.println(list); } }
Output
[1, 2]
No comments:
Post a Comment