I am counting votes for electronic election and I have only one party in my initial version. There will be different threads per voter and the threads will update the vote of a given party.
I decided to use ConcurrentHashMap, but the results are not what I expected...
Map<String, Integer> voting = new ConcurrentHashMap<>();
for (int i = 0; i < 16; i++) {
new Thread(() -> {
voting.put("GERB", voting.getOrDefault("GERB", 0) + 1);
}).start();
}
for (int i = 0; i < 100; i++) {
voting.put("GERB", voting.getOrDefault("GERB", 0) + 1);
}
Thread.sleep(5000); // Waits for the threads to finish
for (String s : voting.keySet()) {
System.out.println(s + ": " + voting.get(s));
}
The result is different every time - it ranges from 114 to 116.
Isn't ConcurrentHashMap supposed to be synchronised?
Well, there's a compound action here. You get the map value given a key, increment it by one, and place it back in the map against the same key. You have to guarantee that all these statements execute atomically. But the given implementation does not impose that prerequisite. Hence you end up with a safety failure.
To fix this, you can use the atomic merge
operation defined in ConcurrentHashMap
. The entire method invocation is performed atomically. Here's how it looks.
Map<String, Integer> voting = new ConcurrentHashMap<>();
for (int i = 0; i < 16; i++)
new Thread(() -> {
voting.merge("GERB", 1, Integer::sum);
}).start();
for (int i = 0; i < 100; i++)
voting.merge("GERB", 1, Integer::sum);
Thread.sleep(5000); // Waits for the threads to finish
for (String s : voting.keySet())
System.out.println(s + ": " + voting.get(s));
Running this program produces the following output:
GERB: 116
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments