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

Dr Heinz M. Kabutz
Dr Heinz M. Kabutz

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

Frequently Asked Questions

How long do I have access to a fully paid course?
How does lifetime access sound? After enrolling, you have unlimited access to this course for as long as you like - across any and all devices you own. Furthermore, if we move to another platform, we will offer you a free transfer of your account for all the courses that you have purchased.
Can I pay via PayPal?
Yes, you can for outright purchases, but not for recurring payments such as paying by installments.
Can I get back the EU VAT?
Absolutely. First off, be sure to enter the VAT number in the appropriate field during the payment process. Then contact Teachable and ask them to reimburse you the VAT.
May I share my login details with my colleagues?
Unfortunately not. The terms of usage are for a single license. Teachable tracks your progress through the curriculum, so you won't know how much you have watched. We offer discounts on bulk licenses by one company. Please contact us for bulk licensing.
May we use the course for running in-house courses?
You may, as long as each of the students in the class has a valid license for that course. For example, some companies run lunch time Design Patterns study groups using our material. This is an effective way to learn. Please contact us for bulk licensing.
What if I am unhappy with the course?
We would never want you to be unhappy! If you are unsatisfied with your purchase, contact us in the first 14 days to tell us what you are unhappy about, and we will give you a full refund and deregister you from the course.
When does the course start and finish?
The course starts now and never ends! It is a completely self-paced online course - you decide when you start and when you finish. We do recommend putting time aside and setting goals to complete the course.
Can I watch the course offline during my commute?
Teachable have an iOS app that lets you watch offline. Android is not supported unfortunately.

Get started now!