线程池+阻塞队列+AtomicLong应用

统计这块主要使用定时线程来进行统计将counter的结果存储到Log文件或者mqsql中

在这块很容易因为一些小的细节出问题:

要将统计结果既存储到log文件又存储到db中,为了保证数据的一致性我们想到了用临时map来存储某个时间的统计结果值,但是在临时map定义的时候就不能定义成Map<String, AtomicLong>了,

如果使用AtomicLong就相当于我们直接把对象类型的数据存入到map中了,以致于调试的时候会发现map的值是变动的,所以在此要多多注意。要不然就像我一样出问题了还不知道咋回事啊!

细节决定成败!

//临时Map来存储结果值

Map<String,Long> tempMap = new HashMap<String,Long>();

//部分代码

synchronized (this.counters) {

Set<Entry<String, AtomicLong>> countersSet = counters.entrySet();

for (Entry<String, AtomicLong> entry : countersSet) {

tempMap.put(entry.getKey(), entry.getValue().get());

}

this.emptyCounter();

}


你可能感兴趣的:(线程池+阻塞队列+AtomicLong应用)