Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
None
-
None
-
None
Description
This code:
import groovy.transform.* @CompileStatic class Counters { public final Map<String,Integer> map = [:].withDefault { 0 } @WithReadLock int get(String id) { map.get(id) } @WithWriteLock void add(String id, int num) { Thread.sleep(100) // emulate long computation map.put(id, map.get(id)+num) } } def counters = new Counters() assert counters.get('a') == 0 assert counters.get('b') == 0 10.times { cpt -> Thread.start { counters.add('a', 1) } def t = Thread.start { Thread.sleep(20) assert counters.get('a') == cpt+1 } t.join(250) }
gives errors such as:
assert counters.get('a') == cpt+1 | | | | | | 2 | 5 6 Counters@3b871f false