Kotiger 最近的时间轴更新
Kotiger

Kotiger

V2EX 第 411094 号会员,加入于 2019-05-10 16:37:35 +08:00
Kotiger 最近回复了
2022-05-20 16:52:20 +08:00
回复了 agzou 创建的主题 Java 请教一个 ConcurrentHashMap 问题
正如四楼大佬所说,contains 和 put 组合在一起就不是安全操作了
public class IdGeneratorService {
private final Map<String, AtomicLong> map = new ConcurrentHashMap<>();

public long nextId(String key) {
// 直接用这个方法
map.computeIfAbsent(key, it->new AtomicLong(0));
return map.get(key).incrementAndGet();
}
}
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1657 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 11ms · UTC 16:38 · PVG 00:38 · LAX 09:38 · JFK 12:38
Developed with CodeLauncher
♥ Do have faith in what you're doing.