67,549
社区成员




public final static int MAP_INIT_CAPACITY = 100000;
public static Map<Integer, Integer> productViewCount = new HashMap<Integer, Integer>(MAP_INIT_CAPACITY);
public static Map<Integer, Integer> providerViewCount = new HashMap<Integer, Integer>(MAP_INIT_CAPACITY);
//每一分钟调用一次flush()
public static void flush() {
ViewCountLogic viewCountLogic = new ViewCountLogic();
Iterator productViewCountIter = productViewCount.keySet().iterator();
while(productViewCountIter.hasNext()) {
Integer productId = (Integer)productViewCountIter.next();//这里抛出异常
Integer viewCount = productViewCount.get(productId);
viewCountLogic.incProductViewCount(productId, viewCount);
ProductLogic productLogic=new ProductLogic();
Product product = productLogic.getProductAllById(productId);
int userId = product.getUserId();
int uViewCount = viewCount;
if(providerViewCount.containsKey(userId)) {
uViewCount += providerViewCount.get(userId);
}
providerViewCount.put(userId, uViewCount);
}
Iterator userViewCountIter = providerViewCount.keySet().iterator();
while(userViewCountIter.hasNext()) {
Integer userId = (Integer)userViewCountIter.next();
Integer viewCount = providerViewCount.get(userId);
viewCountLogic.incUserViewCount(userId, viewCount);
}
productViewCount.clear();
providerViewCount.clear();
productViewCount = new HashMap<Integer, Integer>(MAP_INIT_CAPACITY);
providerViewCount = new HashMap<Integer, Integer>(MAP_INIT_CAPACITY);
}
Iterator<MonthlyStatData> it = papers.iterator();
while (it.hasNext()) {
MonthlyStatData data = it.next();
if (data.getValue == 0 ) {
papers.remove(data);
}
}
上面的代码是说有个月统计的List, 里面有部分统计数据是0, 不想让出现,于是就迭代遍历,删除统计数据是0的数据. 但是上面的代码会导致ConcurrentModificationException, 正确的应该是:
Iterator<MonthlyStatData> it = papers.iterator();
while (it.hasNext()) {
MonthlyStatData data = it.next();
if (data.getValue == 0 ) {
it.remove();
papers.remove(data);
}
}
public static synchronized Map<Integer, Integer> productViewCount = new HashMap<Integer, Integer>(MAP_INIT_CAPACITY);
public static synchronized Map<Integer, Integer> providerViewCount = new HashMap<Integer, Integer>(MAP_INIT_CAPACITY);
public static void incProductViewCount(int productId) {
if(productViewCount.containsKey(productId)) {
int viewCount = productViewCount.get(productId) + 1;
productViewCount.put(productId, viewCount);
} else {
productViewCount.put(productId, 1);
}
}
public static void incProviderViewCount(int userId) {
if(providerViewCount.containsKey(userId)) {
int viewCount = providerViewCount.get(userId) + 1;
providerViewCount.put(userId, viewCount);
} else {
providerViewCount.put(userId, 1);
}
}
public final static int MAP_INIT_CAPACITY = 100000;
public static Map<Integer, Integer> productViewCount = new HashMap<Integer, Integer>(MAP_INIT_CAPACITY);
public static Map<Integer, Integer> providerViewCount = new HashMap<Integer, Integer>(MAP_INIT_CAPACITY);
/*
*
*/
public static void incProductViewCount(int productId) {
if(productViewCount.containsKey(productId)) {
int viewCount = productViewCount.get(productId) + 1;
productViewCount.put(productId, viewCount);
} else {
productViewCount.put(productId, 1);
}
}
/*
*
*/
public static void incProviderViewCount(int userId) {
if(providerViewCount.containsKey(userId)) {
int viewCount = providerViewCount.get(userId) + 1;
providerViewCount.put(userId, viewCount);
} else {
providerViewCount.put(userId, 1);
}
}
/*
*
*/
public static void flush() {
ViewCountLogic viewCountLogic = new ViewCountLogic();
Iterator productViewCountIter = productViewCount.keySet().iterator();
while(productViewCountIter.hasNext()) {
Integer productId = (Integer)productViewCountIter.next();
Integer viewCount = productViewCount.get(productId);
viewCountLogic.incProductViewCount(productId, viewCount);
ProductLogic productLogic=new ProductLogic();
Product product = productLogic.getProductAllById(productId);
int userId = product.getUserId();
int uViewCount = viewCount;
if(providerViewCount.containsKey(userId)) {
uViewCount += providerViewCount.get(userId);
}
providerViewCount.put(userId, uViewCount);
}
Iterator userViewCountIter = providerViewCount.keySet().iterator();
while(userViewCountIter.hasNext()) {
Integer userId = (Integer)userViewCountIter.next();
Integer viewCount = providerViewCount.get(userId);
viewCountLogic.incUserViewCount(userId, viewCount);
}
productViewCount.clear();
providerViewCount.clear();
productViewCount = new HashMap<Integer, Integer>(MAP_INIT_CAPACITY);
providerViewCount = new HashMap<Integer, Integer>(MAP_INIT_CAPACITY);
}