I need to gather some statistics in my software and i am trying to make it fast and correct, which is not easy (for me!)
first my code so far with two classes, a StatsS
Chris Dail's answer looks like a good approach.
Another alternative would be to use a concurrent Multiset
. There is one in the Google Collections library. You could use this as follows:
private Multiset stats = ConcurrentHashMultiset.create();
public void notify ( String key )
{
stats.add(key, 1);
}
Looking at the source, this is implemented using a ConcurrentHashMap
and using putIfAbsent
and the three-argument version of replace
to detect concurrent modifications and retry.