|
| 1 | +CopyOnWriteArrayList — Internal Logic (Short & Clear) |
| 2 | + |
| 3 | +1. Data is stored in a volatile Object[] array. |
| 4 | + Example: array = [A, B, C] |
| 5 | + |
| 6 | +2. Read (get, iteration): |
| 7 | + - Directly reads from current array. |
| 8 | + - No locking required. |
| 9 | + - Iteration works on a snapshot of array at creation time. |
| 10 | + |
| 11 | +3. Write (add, set, remove): |
| 12 | + - Lock is acquired. |
| 13 | + - Current array is copied → new array. |
| 14 | + - Modification applied to new array. |
| 15 | + - Reference switched to new array. |
| 16 | + - Lock released. |
| 17 | + |
| 18 | +Flow: |
| 19 | + |
| 20 | +Initial: |
| 21 | +array → [A, B, C] |
| 22 | + |
| 23 | +Thread 1: add(D) |
| 24 | + Copy old → [A, B, C, D] |
| 25 | + Update ref → array = [A, B, C, D] |
| 26 | + |
| 27 | +Thread 2 (iterating at same time): |
| 28 | + Still sees → [A, B, C] |
| 29 | + |
| 30 | +Result: |
| 31 | +- Readers = fast, no locks. |
| 32 | +- Writers = slow (O(n)), copy required. |
| 33 | + |
| 34 | +Initial: |
| 35 | + arrayRef -> [A, B, C] |
| 36 | + |
| 37 | +Thread 2: iterator it = list.iterator() // snapshot captured now |
| 38 | + it.snapshot -> [A, B, C] |
| 39 | + |
| 40 | +Thread 1: add(D) |
| 41 | + // write operation: |
| 42 | + newArray = copyOf(arrayRef) -> [A, B, C, D] |
| 43 | + arrayRef = newArray // volatile write, atomic reference update |
| 44 | + |
| 45 | +Now: |
| 46 | + arrayRef -> [A, B, C, D] |
| 47 | + it.snapshot -> [A, B, C] // iterator still iterates old snapshot |
| 48 | + |
| 49 | +Initial: |
| 50 | + arrayRef -> [A, B, C] |
| 51 | + |
| 52 | +Thread 1: add(D) |
| 53 | + newArray = copyOf(arrayRef) -> [A, B, C, D] |
| 54 | + arrayRef = newArray |
| 55 | + |
| 56 | +Thread 2: iterator it = list.iterator() |
| 57 | + it.snapshot -> [A, B, C, D] |
| 58 | + |
| 59 | +Now: |
| 60 | + arrayRef -> [A, B, C, D] |
| 61 | + it.snapshot -> [A, B, C, D] // iterator sees the updated array |
| 62 | + |
| 63 | + |
| 64 | +Applications of CopyOnWriteArrayList |
| 65 | + |
| 66 | +1. Read-Mostly Scenarios |
| 67 | + - Ideal when reads are frequent and writes are rare. |
| 68 | + - Best for read-heavy, write-light scenarios. |
| 69 | + - Example: Config data lookup (system settings, constants). |
| 70 | + |
| 71 | +2. Thread-Safe Iteration |
| 72 | + - Iterators never throw ConcurrentModificationException. |
| 73 | + - Example: Multi-threaded logging framework where multiple threads read logs while few append. |
| 74 | + |
| 75 | +3. Observer / Listener Lists |
| 76 | + - Used to store event listeners (like GUI or framework callbacks). |
| 77 | + - Safe to iterate while listeners are being added/removed. |
| 78 | + - Example: Swing, Java concurrency classes use it for listeners. |
| 79 | + |
| 80 | +4. Caching Data |
| 81 | + - Frequently read cached data can be stored without locks. |
| 82 | + - Example: In-memory product catalog that rarely updates but is read by many users. |
| 83 | + |
| 84 | +5. Concurrent Publish-Subscribe |
| 85 | + - Subscribers list is read by many, updated occasionally. |
| 86 | + - Example: Notification systems, message broadcast lists. |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +CopyOnWriteArrayList is best when: |
| 91 | +- Many threads **read data** simultaneously. |
| 92 | +- Few threads **update data** occasionally. |
| 93 | +- Consistency during iteration is important (snapshot iteration). |
0 commit comments