Skip to content

Commit 5bcadb9

Browse files
committed
feat(Demo): add CopyOnWriteArrayList demo with concurrent modification
What - Introduced Demo class showcasing `CopyOnWriteArrayList` usage. - Demonstrates safe iteration and modification of a list while traversing. - Compared to `ArrayList`, which throws `ConcurrentModificationException` on modification during iteration. - Adds shoppingList example where "Butter" is added while reading items. Why - Highlights difference between normal `ArrayList` and thread-safe `CopyOnWriteArrayList`. - Teaches the copy-on-write mechanism: snapshot for readers, fresh copy for each write. - Useful for scenarios where reads dominate writes and thread safety is required. How - Declared `CopyOnWriteArrayList<Integer>` (unused placeholder for demonstration). - Declared `shoppingList` as a `CopyOnWriteArrayList<String>`. - Added initial elements ("Milk", "Eggs", "Bread"). - Iterated with for-each, and while reading "Eggs", added "Butter". - Printed list before and after to show Butter successfully added during iteration. Logic - Inputs: hardcoded items Milk, Eggs, Bread. - Outputs: console print of items during iteration, message "Adding Butter...", and final updated list with Butter included. - Flow: 1. Add 3 items to shoppingList. 2. Print initial list. 3. Iterate and print each item. - If item == "Eggs", add "Butter" to the list. 4. Print updated list at end. - Edge cases: - With `ArrayList`, adding inside loop would throw `ConcurrentModificationException`. - With `CopyOnWriteArrayList`, no exception: iteration uses snapshot, modifications visible only in subsequent iterations or prints. Complexity - Read (iteration, get) is O(1) per operation. - Write (add/remove/set) is O(n) because a new copy of the array is created. - Iteration is safe but iterators do not reflect modifications made after iterator creation. Applications - Ideal for read-heavy, write-light use cases: - Event listener lists. - Caches with infrequent updates. - Configurations that are read often but updated rarely. - Thread-safe alternative to `ArrayList` without explicit synchronization. Notes - CopyOnWriteArrayList consumes more memory on writes due to creating new copies. - For write-heavy workloads, it is inefficient — prefer `ConcurrentLinkedQueue` or other concurrent collections. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent d9798be commit 5bcadb9

File tree

1 file changed

+56
-0
lines changed
  • Section 25 Collections Frameworks/List Interface/Copy On Write ArrayList/src

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.List;
2+
import java.util.concurrent.CopyOnWriteArrayList;
3+
4+
public class Demo {
5+
public static void main(String[] args) {
6+
CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
7+
8+
// List<String> shoppingList = new ArrayList<>();
9+
// Uncomment and see error then comment and run.
10+
11+
List<String> shoppingList = new CopyOnWriteArrayList<>();
12+
shoppingList.add("Milk");
13+
shoppingList.add("Eggs");
14+
shoppingList.add("Bread");
15+
16+
/*
17+
System.out.println("Initial Shopping List: "+shoppingList);
18+
for(String item : shoppingList) {
19+
System.out.println(item);
20+
}
21+
*/
22+
23+
// Read and write both at a time while reading.
24+
System.out.println("Initial Shopping List: "+ shoppingList);
25+
26+
for(String item : shoppingList) {
27+
System.out.println(item);
28+
29+
if(item.equals("Eggs")) {
30+
shoppingList.add("Butter");
31+
System.out.println("Adding Butter while reading.");
32+
}
33+
}
34+
System.out.println("Updated shopping list: "+ shoppingList);
35+
}
36+
}
37+
38+
/*
39+
Single threaded operation and snapshot create karta hai...photocopy of an item or object.
40+
41+
Copy On Write means That whenever a write operation like adding or removing an element,
42+
instead of directly modifying the existing list, a new copy of the list is created,
43+
and the modification is applied to that copy
44+
45+
This ensures that other threads reading the last while it's being modified are being unaffected.
46+
47+
Read Operations: Fast and direct, since they happen-on a static list without interference from modification.
48+
49+
write operations: A new copy of the list is created for every modification.
50+
51+
The reference to the list is then updated so that subsequent(later) reads use this new list.
52+
53+
Notepad --> Notepad copy
54+
55+
Applications Read intensive operations ho.More memory consumption things here.
56+
*/

0 commit comments

Comments
 (0)