Java并发问题 - 使用AtomicInteger计数器映射

时间:2015-01-27 05:45:15

标签: java concurrency synchronization atomic

我有几个线程同时工作以增加地图中的计数器。由于某种原因,以下不适用于计数器有时不增加(在下面的指数是一个枚举类):

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会阻止多个线程同时访问它。感谢。

0 个答案:

没有答案