import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
ConcurrentHashMap<Integer, AtomicInteger> map = new ConcurrentHashMap<Integer, AtomicInteger>(10000)
ConcurrentHashMap<Integer, AtomicInteger> map2 = new ConcurrentHashMap<Integer, AtomicInteger>(10000)
ConcurrentHashMap<Integer, AtomicInteger> map3 = new ConcurrentHashMap<Integer, AtomicInteger>(10000)
ExecutorService es = Executors.newFixedThreadPool(100)
1.upto(10000) {
i ->
es.execute {
AtomicInteger count = map.get(i)
if (count == null) {
count = new AtomicInteger(0)
AtomicInteger oldCount = map.putIfAbsent(i, count)
if (oldCount != null) {
count = oldCount
}
}
count.incrementAndGet()
}
}
for (Integer i = 0; i < 10000; i++) {
es.execute {
AtomicInteger count = map2.get(i)
if (count == null) {
count = new AtomicInteger(0)
AtomicInteger oldCount = map2.putIfAbsent(i, count)
if (oldCount != null) {
count = oldCount
}
}
count.incrementAndGet()
}
}
for (Integer i = 0; i < 10000; i++) {
Integer j = i
es.execute {
AtomicInteger count = map3.get(j)
if (count == null) {
count = new AtomicInteger(0)
AtomicInteger oldCount = map3.putIfAbsent(j, count)
if (oldCount != null) {
count = oldCount
}
}
count.incrementAndGet()
}
}
es.shutdown()
es.awaitTermination(1, TimeUnit.MINUTES)
println map.size()
println map2.size()
println map3.size()