我有几个线程同时工作以增加地图中的计数器。由于某种原因,以下不适用于计数器有时不增加(在下面的指数是一个枚举类):
public class TheoreticalWorkload{
private static final ConcurrentHashMap<Indices, AtomicInteger> WORKLOAD = new ConcurrentHashMap<Indices, AtomicInteger>();
public void update(Indices id, Integer change){
int i = WORKLOAD.get(id).addAndGet(change);
System.out.println(i);
}
}
我做错了什么?我认为将计数器声明为AtomicInteger会阻止多个线程同时访问它。感谢。