Commit 5bcadb9
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 changedLines changed: 56 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
0 commit comments