JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label CopyOnWriteArrayList. Show all posts
Showing posts with label CopyOnWriteArrayList. Show all posts

Difference between CopyOnWriteArrayList and ArrayList in Java.

2:50 AM
CopyOnWriteArrayList and ArrayList in Java.  we have seen What is CopyOnWriteArrayList in Java and How it achieves thread-safety by creating a separate copy of List for each writes operation. Now let's see Some difference between ArrayList and CopyOnWriteArrayList in Java, which is another implementation of List interface : 1) First and foremost difference between CopyOnWriteArrayList and ArrayList in Java is that CopyOnWriteArrayList is a thread-safe collection while ArrayList is not thread-safe and can not be used in the multi-threaded environment. 2) The second difference between ArrayList and CopyOnWriteArrayList is that Iterator of ArrayList is fail-fast and throw ConcurrentModificationException once detect any modification in List once iteration begins but Iterator of CopyOnWriteArrayList is fail-safe and doesn't throw ConcurrentModificationException. 3) The third difference between CopyOnWriteArrayList vs ArrayList is that Iterator of former doesn

What is CopyOnWriteArrayList or CopyOnWriteArrayList vs Array List in Java-Tutorial

2:48 AM
CopyOnWriteArrayList vs Array List in Java CopyOnWriteArrayList is a concurrent Collection class introduced in Java 5 Concurrency API along with its popular cousin ConcurrentHashMap in Java. CopyOnWriteArrayList implements List interface like ArrayList, Vector, and LinkedList but its a thread-safe collection and it achieves its thread-safety in a slightly different way than Vector or other thread-safe collection class. As the name suggests CopyOnWriteArrayList creates a copy of underlying ArrayList with every mutation operation e.g. add, remove, or when you set values. That's why it is only suitable for a small list of values which are read frequently but modified rarely e.g. a list of configurations. Normally CopyOnWriteArrayList is very expensive because it involves costly Array copy with every writes operation but it's very efficient if you have a List where Iteration outnumbers mutation e.g. you mostly need to iterate the ArrayList and don't modify it too often

.