Atomically incrementing counters stored in ConcurrentHashMap

前端 未结 6 1466
猫巷女王i
猫巷女王i 2020-12-05 04:02

I would like to collect some metrics from various places in a web app. To keep it simple, all these will be counters and therefore the only modifier operation is to incremen

6条回答
  •  一个人的身影
    2020-12-05 04:40

    In Java 8:

    ConcurrentHashMap map = new ConcurrentHashMap<>();
    
    map.computeIfAbsent("key", k -> new LongAdder()).increment();
    

提交回复
热议问题