Atomic Integer incrementAndGet() 方法线程安全吗?我没有看到其中有任何 synchronized 关键字的使用。我正在使用以下代码生成唯一 ID:
public enum UniqueIdGenerator {
INSTANCE;
private AtomicLong instance = new AtomicLong(System.currentTimeMillis());
public long incrementAndGet() {
return instance.incrementAndGet();
}
}
我想知道调用该方法以生成唯一 ID 的多个线程是否会导致任何问题。
UniqueIdGenerator.INSTANCE.incrementAndGet()
谢谢!
最佳答案
是的,是的。它使用基于名为 Unsafe
关于java - Atomic Integer incrementAndGet() 线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44053300/