Good afternoon from my beautiful Island of Crete!
A few weeks ago, I was thinking about the common saying: "Don't write your own concurrency code, rather use java.util.concurrent." Of course I agree with that sentiment. It is too easy to make mistakes and when we do, we don't have billions of users like the JVM does. What makes the java.util.concurrent classes so rock solid is that they are baked into the JVM so that everyone uses them, whether they like it or not. Bugs are discovered and repaired.
But that led me to another thought. I have a good high-level view of how these concurrency constructs work, having studied and taught them for almost two decades. However, I had never gone through them line by line, to figure out precisely how they work.
When Helene was studying English at university, she had to read Chaucer, the father of English literature. For years she would amuse me by quoting his Old English prose. Most English professors make their students dig through thick volumes of Milton and Shakespeare. Similarly, we can greatly advance our Java knowledge by an in-depth study of the JDK source code.
As I was contemplating this, I settled on the ArrayBlockingQueue as a good place to start. And no, it had nothing to do with it being the first alphabetically. I had already written a course on how to write your own CircularArrayList (get your free copy here). I also knew that the synchronization inside the class was straightforward. A single ReentrantLock with two Conditions, notFull and notEmpty.
I had no idea what I was getting into. I spent days poring over the code, making sure that I knew not just the what but also the why. I looked at the change lists in both CVS (remember that?) and the OpenJDK GitHub. I wrote tests to compare ArrayBlockingQueue with other BlockingQueues and with older versions of itself. There are a lot of fascinating tricks in this class, from weak references to locking for visibility to split for-loops to calling the precise number of needed signal() instead of signalAll(). And you should see how they implemented weakly consistent iteration!
It was fun! I then recorded everything as an in-depth walkthrough so I can share it with you. This means you don't have to spend days trying to figure things out and guess why the weird for-loop. You can sit back with a cup of hot coffee, your IDE and my course playing on your second monitor, as we go through the code together. So much more fun than doing this alone.
The complete walkthrough takes just under 4 hours. At the end of that, not only will you have an intimate knowledge of how the ArrayBlockingQueue works (FWIW), but more importantly, you will have learned how it is made. That part is truly fascinating. Perhaps in a couple of years time we will have a completely teardown of all the classes in java.util.concurrent?
I am selling the 4-hour ArrayBlockingQueue course for $50 on JavaSpecialists.Teachable.com. I believe you will get far more value than $50 from the course and as always, I am happy to refund 100% within 30 days. You also have the option of paying in 5 instalments.
This course is part of my new Java Specialists Superpack 21. (Yes, I've dropped the "20" from my Superpack. Who wants another 2020?)
I am hoping that you will join me in this fascinating adventure of doing a teardown of the ArrayBlockingQueue.