I am writing share something I implemented recently, to monitor streaming operations into the Voldemort server. What I needed was a simple avg and event rate counter
The idea is very simple. Have atomic longs for the event value sum and event count, to be updated by the application threads. Whats the big deal right? The deal (not sure how big, depends on who you are) is to have this counter reset itself, every so often. This is a very common usage pattern in data systems, where we would like to monitor performance over small periods of time (so we get enough samples to smoothen the outliers) and reset every so often. Also, one should make sure the "monitoring" thread always reports statistics for a complete interval.
The trick is to avoid resetting the atomic long every interval, since doing so would mean missing some concurrent updates from other application threads in the meantime. Also, since we are maintaining state about the previous interval, it becomes necessary to synchronize briefly on the state update, between the monitoring thread and the one application thread that gets to do the honors of transitioning to the next interval.
Let me now cut to the chase and present the code itself, which is fairly well documented
A very interesting next challenge would be to implement a non-blocking Histogram. Or atleast I am very interested in studying such implementations.