Friday, October 11, 2013

Phaser in Java 7 : Concurrency

To really understand what the Phaser class does and why it is useful and how it is different from CountDownLatch, Cyclic Barrier and what problem it is going to solve. Let have a short review of each synchronizers  in terms of 
  • Number of parties : Number of parties is another way of saying number of different threads
  • Reusable : It means you don't have to create a new instance of the barrier ( i.e, Countdownlatch  cyclic barrier, Phaser)  before reusing.
  • Advanceable : It means a thread can arrive and continue doing work without waiting for others threads or without waiting for all threads to complete

  • Fixed number of parties 
  • Not reusable as you have to create another CountDownLatch for each synchronize point
  • It is Advanceable as latch.countDown() do the advance and latch.await() do the waiting part
  • Fixed number of parties.
  • Reusable that how it differ from CountDownLatch but all threads must wait for each party to arrive at the barrier.
  • Not advanceable

  • Dynamic number of parties
  • Reusable
  • Advanceable : phaser.arrive() that how it advance

Phaser is a useful feature when few independent threads have to work in phases to complete a task. So, a synchronization point is needed for the threads to work on a part of a task, wait for others to complete other part of the task, and do a sync-up before advancing to complete the next part of the task.

Before going for an example, have a look on how Phaser do the registration and Synchronization. There are also other step involved in Phaser such as Termination, Tiering and Monitoring, which we look later may after some time.

Registration : Unlike the case for other barriers, the number of parties registered to synchronize on a phaser may vary over time. Tasks may be registered at any time (using methods register(), bulkRegister(int), or forms of constructors establishing initial numbers of parties), and optionally deregistered upon any arrival (using arriveAndDeregister()).

Synchronization : Like a CyclicBarrier, a Phaser may be repeatedly awaited. Method arriveAndAwaitAdvance() has effect analogous to CyclicBarrier.await. Each generation of a phaser has an associated phase number.The phase number starts at zero, and advances when all parties arrive at the phaser, wrapping around to zero after reaching Integer.MAX_VALUE

Methods in Phaser class

Phaser(int numThreads)
Creates a Phaser object with a given number of threads (parties) to arrive to advance to the next stage; the initial phase is set to 0.
Int register()
Adds a new thread (party) to this Phaser object. Returns the phase current number. Throws an IllegalStateException if the maximum supported parties are already registered.
Int bulkRegister(int numThreads)
Adds numThreads of unarrived parties to this Phaser object. Returns the phase current number. Throws an IllegalStateException if maximum supported parties are already registered
Int arrive()
Arrives at this phase without waiting for other threads to arrive. Returns the arrival phase number. Can throw an IllegalStateException.
Int arriveAndDeregister()
Same as the previous method, but also deregisters from the Phaser object.
Int arriveAndAwaitAdvance()
Arrive at this phase and waits (i.e., blocks) until other threads arrive
Int awaitAdvance(int phase)
Waits (i.e., blocks) until this Phaser object advances to the given phase value.
Int getRegisteredParties()
Returns the number of threads (parties) registered with this Phaser object.
Int getArrivedParties()
Returns the number of threads (parties) arrived at the current phase of the Phaser object
Int getUnarrivedParties()
Returns the number of threads (parties) that have not arrived when compared to the registered parties at the current phase of the Phaser object


Let take an example of processing a delivery order in coffee shop. Assume there are 3 workers: a cook, a waiter, an attendent. To simlify this problem assume each delivery of order consist of 3 food items. Completing a delivery order consists of preparing the three orders one after another. To complete preparing a food item, all three workers—the cook, the helper, and the attendant—should do their part of the work.

Sample Output
Starting to process the order.
cook  doing this work for order 1
attendent  doing this work for order 1
waiter  doing this work for order 1
order 1 completed
attendent  doing this work for order 2
cook  doing this work for order 2
waiter  doing this work for order 2
order 2 completed
attendent  doing this work for order 3
cook  doing this work for order 3
waiter  doing this work for order 3
order 3 completed
Process completed

If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...


  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.