Java: map of concurrently incremented counters -


i need implement map of counters (like map) in application. structure supposed accessed several threads.

it looks concurrenthashmap<key, long> not proper solution, right?

i thought concurrenthashmap<key, atomiclong> instead.

but there problem - requests increment not spread evenly. few popular keys have 95% of increment requests data structure.

as far understand lead concurrent access single atomiclong instances , there many locks should occur decrease efficiency.

question 1: there better solution - perhaps, better data type instead of atomiclong, allows short accumulation of increments or this?

question 2: want persist structure disk periodically (perhaps, every minute), , want persist "actual" state (with recent updates settled?) - straightforward way it?

what makes think atomiclong uses locks internally? not true, it's built on cas operations. advice implement atomiclong , profile implementation later. if (and if) counter bottleneck, consider replacing other implementation.

"we should forget small efficiencies, 97% of time: premature optimization root of evil" - donald knuth

as of state persistence simplest approach serialize map:

bytearrayoutputstream out = new bytearrayoutputstream(); objectoutputstream objout = new objectoutputstream(out); objout.writeobject(map); objout.close(); 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -