Teardown 02 - Copy-on-Write Array List and Set
Thread-safe collections with snapshot iterators
"Rather use java.util.concurrent
than write your own"
Who has not heard this sage advice?
Of course we agree, and follow this advice ourselves. But what if the constructs in java.util.concurrent
differ from what we need? Can we write our own? And what can we learn from studying how the java.util.concurrent
classes work?
In our Teardown series we study the classes in java.util.concurrent
line-by-line. We analyze every for-loop and if statement, asking why the authors wrote it this way. We dissect the concurrency controls, looking at how the authors apply it to the construct.
The 2nd teardown course looks at the CopyOnWriteArrayList
and its close cousin, the CopyOnWriteArraySet
. Most of the concurrency controls in this class are fairly easy, but we do have delve into releaseFence for visibility and some odd volatile writes for maintaining order. The class used to use the ReentrantLock
for synchronization, but in Java 9 this was changed to synchronized. These classes are useful when we need a thread-safe list that gives us a snapshot iterator. A lot of care has been taken to avoid synchronization unless it is necessary. For example, in the remove(Object)
method, we first try find the index of the given object in the list. If we cannot find it, then we immediately leave. We then synchronize and verify that the list has not changed. If it has, then we need to search again.
This mini-course consists of almost 4 hours of detailed instruction of how the CopyOnWrite collections work. Watch it either from start to finish, or jump around between sections as your curiosity is piqued. You will definitely learn something - or your money back!
(Make sure to also check out our CopyOnWriteArrayList/Set Teardown)
Your Instructor
Heinz Kabutz is the author of The Java Specialists’ Newsletter, a publication enjoyed by tens of thousands of Java experts in over 150 countries. He has been a Java Champion since 2005.
Course Curriculum
-
Preview2. CopyOnWriteArrayList overview (4:11)
-
Preview3. Interfaces implemented by COWAList (1:32)
-
Preview4. Serialization (5:21)
-
Preview5. Synchronized vs ReentrantLock (2:15)
-
Start6. Volatile array (0:37)
-
Start7. getArray() and setArray() (1:11)
-
Start8. Package private lock (1:49)
-
Start9. Constructors (14:16)
-
Start10. size() and isEmpty() (0:26)
-
Start11. indexOf() and contains() (4:35)
-
Start12. clone() (9:41)
-
Start13. toArray() (2:00)
-
Start14. Helper functions (1:28)
-
Start15. get() and set() (10:59)
-
Start16. add(E) (0:21)
-
Start17. add(index, E) (2:01)
-
Start18. remove(index) (10:37)
-
Start19. remove(Object) (21:31)
-
Start20. removeRange() (1:55)
-
Start21. addIfAbsent() (4:48)
-
Start22. containsAll() (12:51)
-
Start23. removeAll(), retainAll(), removeIf(), bulkRemove() (11:41)
-
Start24. addAllAbsent() (3:40)
-
Start25. clear() (0:12)
-
Start26. addAll(Collection) (1:39)
-
Start27. addAll(index, Collection) (1:50)
-
Start28. forEach() (0:26)
-
Start29. replaceAll() (0:58)
-
Start30. sort() (0:57)
-
Start31. toString(), equals(), hashCode() (3:13)
-
Start32. Iteration (3:25)
-
Start33. SubList (28:23)
-
Start34. Conclusion to COWArrayList (2:42)
-
Start1. CopyOnWriteArraySet Introduction (1:56)
-
Start2. CopyOnWriteArraySet Motivation (13:06)
-
Start3. Constructors (1:38)
-
Start4. Delegated methods (0:16)
-
Start5. containsAll() with experiment (21:49)
-
Start6. More delegated methods (1:08)
-
Start7. (Bonus) Modernising CopyOnWriteArrayList and CopyOnWriteArraySet (18:23)
-
Start8. Final words (1:22)