Teardown 01 - ArrayBlockingQueue
Learn to code like a pro by studying how pros code
"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 first construct we chose to study is the ArrayBlockingQueue
.
The concurrency controls of this class are one of the easiest to understand. ArrayBlockingQueue
has one ReentrantLock
with two Condition
s notEmpty and notFull. Unfortunately the weakly consistent iterators are somewhat complex. Furthermore, the authors overcomplicated their work by not keeping one space free, like we see in ArrayDeque
. This means that when takeIndex==putIndex
, the queue is either full or empty.
This mini-course consists of almost 4 hours of detailed instruction of how ArrayBlockingQueue
works. 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!
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
-
Preview01. Introduction to ArrayBlockingQueue (2:01)
-
Preview02. Interfaces - Collection (1:22)
-
Preview03. Interfaces - Queue (3:36)
-
Preview04. Interfaces - BlockingQueue (3:08)
-
Preview05. Fields (3:55)
-
Start06. invariantsSatisfied() (5:49)
-
Start07. ReentrantLock and Conditions (2:26)
-
Start08. Why do we need Itrs? (5:42)
-
Start09. Constructors (2:23)
-
Start10. Lock in Constructor for Visibility (3:42)
-
Start11. add() (0:34)
-
Start12. offer() (1:01)
-
Start13. put() - lockInterruptibly (3:49)
-
Start14. put() condition predicate with while (2:11)
-
Start15. offer() with timeout awaitNanos() (4:58)
-
Start16. poll() (0:25)
-
Start17. take() (0:19)
-
Start18. poll() with timeout (0:56)
-
Start19. peek() (1:41)
-
Start20. element() and remove() (0:48)
-
Start21. commented out assertions inside enqueue() (4:16)
-
Start22. enqueue() (1:23)
-
Start23. dequeue() (2:30)
-
Start24. removeAt() (6:45)
-
Start25. inc(), dec(), itemAt() (3:28)
-
Start26. size() - would a ReadWriteLock be better? (3:04)
-
Start27. remainingCapacity() (0:21)
-
Start28. remove(Object) with split nested for loop (9:44)
-
Start29. contains(Object) (1:44)
-
Start30. toArray() (4:14)
-
Start31. toArray(T[]) (4:27)
-
Start32. Helpers Facade (9:03)
-
Start33. Navigating source history with GIT and CVS (6:29)
-
Start34. Helpers for printing Map entries (0:50)
-
Start35. clear() (3:33)
-
Start36. signal() vs signalAll() (7:23)
-
Start37. drainTo() (6:11)
-
Start38. spliterator() (2:07)
-
Start39. forEach() (1:31)
-
Start40. removeIf(), removeAll(), retainAll(), bulkRemove() (7:14)
-
Start41. bulkRemoveModified() (12:18)
-
Start42. checking invariants (1:31)
-
Start43. Itrs list of iterators (32:05)
-
Start44. Itr iterator implementation (37:12)
-
Start45. Final words about ArrayBlockingQueue (2:31)
-
Start46. TimeUnit (11:53)