In this blog post we will discuss about one of the very important java interview question Describe CopyOnWriteArrayList? Where is it used in Java Applications? We will look into some practical examples so that below points would me clear to you about CopyOnWriteArrayList.
- Functionality wise this collection is very much similar to ArrayList except the fact that CopyOnWriteArrayList is thread-safe.
- It maintains thread-safety using Immutability approach, where any modification operations results in creating a fresh copy of the underlying array.
- Using CopyOnWriteArrayList is too costly but may be more efficient than alternatives when traversal operations vastly with updating and is useful when you don’t want to use synchronize traversals.
- CopyOnWriteArrayList creates the “snapshot” style iterator and uses a reference to the state of the array at the point that the iterator was created. Underlying array of CopyOnWriteArrayList never changes during the entire lifetime of the iterator and that’s way interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException.
- Element-changing operations in CopyOnWriteArrayList using iterators themselves (remove, set, and add) are not supported. These methods throw UnsupportedOperationException.
Example which will throw ConcurrentModificationException known as Fail-Fast Iterators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.kkjavatutorials.client; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class CopyOnWriteArrayListTest_1 { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add("A"); list.add("B"); Iterator<String> itr = list.iterator(); while (itr.hasNext()) { String element = itr.next(); if(element.equals("A")){ list.add("AA"); } } System.out.println(list); } } |
Above program will throw Exception as below:
1 2 3 4 |
Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901) at java.util.ArrayList$Itr.next(ArrayList.java:851) at com.kkjavatutorials.client.CopyOnWriteArrayListTest_1.main(CopyOnWriteArrayListTest_1.java:19) |
Example which will not throw ConcurrentModificationException known as Fail-Safe Iterators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.kkjavatutorials.client; import java.util.Iterator; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public class CopyOnWriteArrayListTest_2 { public static void main(String[] args) { List<String> list = new CopyOnWriteArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add("A"); list.add("B"); Iterator<String> itr = list.iterator(); while (itr.hasNext()) { String element = itr.next(); if(element.equals("A")){ list.add("AA"); } } System.out.println(list); } } |
Above program will produce below output without any Exception:
1 |
[A, B, C, A, B, AA, AA] |
You may also like:
What are common multi-threading issues faced by Java Developers?
What are different states of a Thread? What does those states tell us?
What happens when wait() & notify() methods are called?
What is difference between sleep(), yield() and wait() method?
What is difference between Callable and Runnable Interface?
That’s all about Describe CopyOnWriteArrayList? Where is it used in Java Applications?
If you have any feedback or suggestion please feel free to drop in blow comment box.