主题 : 点击数 的线程问题
级别: 童生
UID: 6682
积分:30 加为好友
威望: 0 精华: 0
主题:2 回复:15
注册时间:2010-05-12
在线时长:0
1#   发表于:2012-07-16 17:18:10  IP:58.222.*.*
public Long setViewCount(Integer topicId) {
Long viewCount = 0L;
Element e = cache.get(topicId);
if (e != null) {
viewCount = (Long) e.getValue() + 1;
} else {
BbsTopic topic = bbsTopicMng.findById(topicId);
if (topic.getViewCount() == null) {
viewCount = 1L;
} else {
viewCount = topic.getViewCount() + 1;
}
}
cache.put(new Element(topicId, viewCount));
refreshToDB(topicId);
return viewCount;
}


如果两个线程同时访问,这代码一定有线程问题, 即线程同时访问viewCount = (Long) e.getValue() + 1;得到的则是相同的值,则最终少了一个点击数
1 共1页