This question was asked two years ago, but the resources it mentions are either not very helpful (IMHO) or links are no longer valid.
There must be some good tutorials out there to understand Phaser
. I've read the javadoc, but my eyes glaze over, since in order to really understand the javadoc you kind of have to know how these classes are supposed to be used.
Anyone have any suggestions?
For Phaser I have answered a few questions. Seeing them may help in understanding their applications. They are linked at the bottom. But to understand what the Phaser does and why its useful its important to know what it solves.
Here are attributes of a CountdownLatch and CyclicBarrier
Note:
CountdownLatch
latch.countDown();
advanceable
latch.await();
must wait )CyclicBarrier
So the CountdownLatch is not reusable, you must create a new instance each time, but is avanceable. CylicBarrier can be re used but all threads must wait for each party to arrive at the barrier.
Phaser
When a thread wants to be known to the Phaser they invoke phaser.register()
when the thread arrives at the barrier they invoke phaser.arrive()
and here is where it is advanceable. If the thread wants to await for all registered tasks to complete phaser.arriveAndAwaitAdvance()
There is also the concept of a phase in which threads can wait on a completion of a other operations that may have not completed. Once all threads arrive at the phaser's barrier a new phase is created (an increment of one).
You can take a look at my other answers, maybe it will help:
Java ExecutorService: awaitTermination of all recursively created tasks