Sunday, August 28, 2011

Guicing up your app with Google Guice

I've created a simple but not completely trivial demo application using Guice. This is something I did to understand and compare Google-Guice with JBoss Weld's CDI RI for Java SE.

This project is a pure Guice project called Guice-Demo (duh) . Here's the source. Well, that's all there is. I thought this would be of some use to other first time users. The project wiki/docs are minimal and I might add some info later on, when I have the time. Until then I hope that the code is self explanatory.

If you are interested in the Java CDI standard, then this is useful. Weld also works on JavaSE but has no control over classpath scanning for injection and makes starts up times slower and somewhat wasteful.

Guice's module creation is very straightforward. It also allows creating multiple Injector instances.

Both Weld and Guice are not very unit test friendly as you will see here.


Ashwin.

Saturday, August 27, 2011

Hiking in Stevens Creek County Park

Accessible and a relaxing walk around the reservoir that can easily convert to a longer hike. It's surprising that I had never been to this place, less than 15 minutes away. Avoid the main entrance on Balboa road which requires a fee to park. Try the West campus entrance where street parking is allowed (looked like).

There are 2 adjacent parks that are probably worth visiting as well.



Saturday, August 13, 2011

In-memory DB tables, shared nothing, time series, low latency and others

Some useful articles I read in the past few weeks.

Big data, shared nothing, distributed SQL, in-memory SQL:

New age time series databases:
Very good and detailed accounts of low latency Java based servers:
Disk I/O and time related notes.

Programming and Java:

Until next time,
Ashwin.

Monday, August 01, 2011

Java 7's j.u.c.Phaser - a short tutorial

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:

  1. Parallel operations need to proceed in lockstep
  2. After every step, all parallel operations wait until all others have completed
  3. When they do, all proceed to the next step and so on...
Sounds easy, but understanding the API was not easy for me. So, I had to write a simple program to see how it worked. Here it is:


What the program does is:
  1. Create a Phaser instance and set it up to expect 2 parties and the main thread/itself as a third party
  2. Start 2 producer threads
  3. Start a consumer thread
  4. Unregister itself as the third party and let the 2 producers run in parallel - as 2 remaining parties
  5. In the mean while the 2 producer threads pretend to do some work and move forward in phases
    1. There are a total of 10 phases
    2. Each producer writes it results into an array where the array position matches the phase number
    3. I call this array per producer - a lane (like a highway)
  6. While the producers are running, the consumer tries to catch up with them
    1. It waits for the phase to complete and then reads the results of that phase
    2. The consumer is slower and over time it trails behind the producers
    3. When it comes back after pretending to do some processing the producers would've moved - sometimes 1 or even 2 phases ahead
    4. The consumer now reads all the phases that have completed so far and then catches up
  7. 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!