Kotiger's recent timeline updates
Kotiger

Kotiger

V2EX member #411094, joined on 2019-05-10 16:37:35 +08:00
Kotiger's recent replies
May 20, 2022
Replied to a topic by 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();
}
}
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1015 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 16ms · UTC 19:03 · PVG 03:03 · LAX 12:03 · JFK 15:03
♥ Do have faith in what you're doing.