Thread Safety with Phaser
A detailed look at how the Java 7 Phaser works
CountDownLatch is easy to understand, but hard to use. Phaser is the opposite. Hard to understand, easy to use. With CountDownLatch, we only have two methods: await() and countDown(). Phaser endows us with: register(), arrive(), arriveAndAwaitAdvance(), arriveAndDeregister(), awaitAdvance(), awaitAdvanceInterruptibly() and a bunch of management methods. In this mini-course we spend an hour explaining the intricacies of Phaser.
It is for Java programmers who are already familiar with thread safety programming. We also show a demo that compares the performance of coordinated tasks using wait/notify, CountDownLatch, CyclicBarrier, volatile spin loops and Phaser. In the end, Phaser performs the best with the most simple code. We then go into tiered Phasers, an approach to further reduce thread contention. We end with a look at ManagedBlocker and how this allows Phaser to work with parallel streams.
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.