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.
Heinz Kabutz is the author of The Java Specialists’ Newsletter, a publication enjoyed by tens of thousands of Java experts in over 145 countries. His book “Dynamic Proxies (in German)” was #1 Bestseller on Amazon.de in Fachbücher für Informatik for about five minutes until Amazon fixed their algorithm. Thanks to a supportive mother, he has now sold 5 copies.
Heinz’s Java Specialists’ newsletter is filled with amusing anecdotes of life on the Island of Crete. He is a popular speaker at all the best Java conferences around the world, and also at some of the worst. He teaches Java courses in classrooms around the world, where his prime objective is to make absolutely sure that none of his students fall asleep. He is not always successful.