This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Your observations are right on the spot. This example is horrible. The correct way (in my opinion) would be to use AtomicInteger as a value, then use ConcurrentMap.putIfAbsent() to make sure each key is inserted only once and AtomicInteger.incrementAndGet() to increment the counter atomically.
I'd suggest to use Sun/Oracle's tutorials preferentially. Concurrency is covered here, though admittedly it does not go very deep. The quality of other tutorials is hard to evaluate, especially as tutorials are mostly read by beginners. Oracle's tutorial coupled with thorough reading of JDK classes' javadoc are probably better than relying on questionable third-party sources. The web is full of similar "pearls of wisdom", unfortunatelly.
Joined: Jan 28, 2010
Hi thanks for your reply. I agree with your approach. As far as concurrency resources go, I have just read "Java Concurrency in Practice" and it's the best book I've read. I think anything Josh Bloch is involved in is top quality.
subject: Is this a correct use of ConcurrentHashMap?