private final static Logger logger = LoggerFactory.getLogger(ComtradeService.class);
private Map<String, ComtradeCache> cache = new TreeMap<String, ComtradeCache>();
private Map<String, ComtradeSeparator> cmtdSep = new TreeMap<String,ComtradeSeparator>();
/**
* 超时COMTRADE内存清理
*/
private void comtradeGC() {
long now = System.currentTimeMillis();
// Iterator<Map.Entry<String,ComtradeCache>> it = cache.entrySet().iterator();
// while(it.hasNext()){
// Map.Entry<String, ComtradeCache> entry = it.next();
// if (now - entry.getValue().getTime() > 5*1000){
// it.remove();
// }
// }
for(Entry<String, ComtradeCache> entry : cache.entrySet()) {
if(now - entry.getValue().getTime() > 5 *1000) {
cache.remove(entry.getKey());
cmtdSep.remove(entry.getKey());
}
}
private Map<String, ComtradeCache> cache = new TreeMap<String, ComtradeCache>();
private Map<String, ComtradeSeparator> cmtdSep = new TreeMap<String,ComtradeSeparator>();
/**
* 超时COMTRADE内存清理
*/
private void comtradeGC() {
long now = System.currentTimeMillis();
// Iterator<Map.Entry<String,ComtradeCache>> it = cache.entrySet().iterator();
// while(it.hasNext()){
// Map.Entry<String, ComtradeCache> entry = it.next();
// if (now - entry.getValue().getTime() > 5*1000){
// it.remove();
// }
// }
for(Entry<String, ComtradeCache> entry : cache.entrySet()) {
if(now - entry.getValue().getTime() > 5 *1000) {
cache.remove(entry.getKey());
cmtdSep.remove(entry.getKey());
}
}
}
网上的搜索的结果是map和list采用remove()后,modCount不等于expectedModCount,则抛出ConcurrentModificationException异常。
原理解释:https://www.cnblogs.com/dolphin0520/p/3933551.html。
避免的方法是采用迭代器的remove()方法。