Here’s a great look at what Java 8 brought to ConcurrentHashMaps, including close looks at the methods at your disposal and their performance impacts.

Whilst doing some refactoring on updates to ConcurrentHashMap values, I came across these great articles …

… and was inspired to try to develop the theme a bit further.

Pre-Java 8, we had various ways to try to perform atomic operations on the values of Concurrent collections as described by Dima.

For example, a simple counter:

Java

1

// Incrementing a count of the occurrences of a currency symbol

2

// (In reality we would have used an atomic variable even pre Java 8)

3

ConcurrentHashMap <String Integer> map = new ConcurrentHashMap <>();

4

String key = "USD/JPY";

5

Double oldValue; Double newValue; double increment = 1.0;

6

do {

7

    oldValue = results.get(key);

8

    newValue = oldValue == null? increment: oldValue + increment;

9

} while (!results.replace(key, oldValue, newValue));

#java #tutorial #java 8 #java performance #concurrenthashmap #atomic updates

Java 8: ConcurrentHashMap Atomic Updates
1.55 GEEK