fix ExecutorRouteRound count++ concurrency problems, use AtomicInteger replace Integer

pull/22/MERGE
jinghui.lin 4 years ago
parent b99382d114
commit 15d8bcd517

@ -8,14 +8,16 @@ import java.util.List;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by xuxueli on 17/3/10.
*/
public class ExecutorRouteRound extends ExecutorRouter {
private static ConcurrentMap<Integer, Integer> routeCountEachJob = new ConcurrentHashMap<Integer, Integer>();
private static ConcurrentMap<Integer, AtomicInteger> routeCountEachJob = new ConcurrentHashMap<>();
private static long CACHE_VALID_TIME = 0;
private static int count(int jobId) {
// cache clear
if (System.currentTimeMillis() > CACHE_VALID_TIME) {
@ -23,11 +25,16 @@ public class ExecutorRouteRound extends ExecutorRouter {
CACHE_VALID_TIME = System.currentTimeMillis() + 1000*60*60*24;
}
// count++
Integer count = routeCountEachJob.get(jobId);
count = (count==null || count>1000000)?(new Random().nextInt(100)):++count; // 初始化时主动Random一次缓解首次压力
AtomicInteger count = routeCountEachJob.get(jobId);
// 初始化时主动Random一次缓解首次压力
if (count == null || count.get() > 1000000) {
count = new AtomicInteger(new Random().nextInt(100));
} else {
// count++
count.addAndGet(1);
}
routeCountEachJob.put(jobId, count);
return count;
return count.get();
}
@Override

Loading…
Cancel
Save