If you've tried to figure out how the Phaser works in Java 7 and haven't had much luck, then you are not alone. I too had some difficulty understanding what looks like a very esoteric concurrency construct. I asked around on the Concurrency-Interest forum. Folks there are very helpful and I almost understood it but was missing some details.
So, I decided to get my hands dirty and play with this new toy. Phaser (as the JavaDocs say) is very much like a CountDownLatch or a CyclicBarrier but is better suited where:
- Parallel operations need to proceed in lockstep
- After every step, all parallel operations wait until all others have completed
- When they do, all proceed to the next step and so on...
What the program does is:
- Create a Phaser instance and set it up to expect 2 parties and the main thread/itself as a third party
- Start 2 producer threads
- Start a consumer thread
- Unregister itself as the third party and let the 2 producers run in parallel - as 2 remaining parties
- In the mean while the 2 producer threads pretend to do some work and move forward in phases
- There are a total of 10 phases
- Each producer writes it results into an array where the array position matches the phase number
- I call this array per producer - a lane (like a highway)
- While the producers are running, the consumer tries to catch up with them
- It waits for the phase to complete and then reads the results of that phase
- The consumer is slower and over time it trails behind the producers
- When it comes back after pretending to do some processing the producers would've moved - sometimes 1 or even 2 phases ahead
- The consumer now reads all the phases that have completed so far and then catches up
- This consumer looks like it is reading "read-committed" transactions - doesn't it?
This picture (hopefully) explains more clearly, what is happening in the code:
(Remember - the sequence diagram and log shown here are specific to my computer. On slower or faster machines you might see slightly different results - for the consumer, at least in the beginning phases.)
The log output:
Until next time!
5 comments:
Good attempt at introducing the newer features. I do ask questions on that list too but mine are more basic.
I am trying to figure out FJ to start with. I am also planning to buy time on GAE for example to run on multiple cores. I think I have to use my credit card. If it is not too expensive and I have enough information I will present some details at a small conference in bangalore.
Good attempt at introducing these hard features. I do ask questions on this list too.
Currently I am trying to figure out FJ. I was planning to run the samples on the cloud if I am able to afford it.
Why did you start the sleep method with a dollar sign ($)? I believe you couldn't do this before Java SE 7, but does it provide any different functionality or is it a new naming standard?
It's just a naming convention I follow - I usually keep such public-static helper methods in a separate class.
Starting a method or variable name with $ or _ is legal syntax in Java....from the beginning.
And since public-static methods can be static-imported from Java 5 onwards, I start such method names with a $ to make it easier for readers to understand that the method has been imported from another class. In this example, it's all in the same single class - not of much use.
Cheers!
Good attempt at introducing these hard features. I do ask questions on this list too.
Post a Comment