Commit 02ac02b3 by Nepxion

优化成ThreadLocalRandom

parent 2fac9dae
......@@ -11,7 +11,7 @@ package com.nepxion.discovery.plugin.framework.decorator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
......@@ -35,8 +35,6 @@ public class ZoneAvoidanceRuleDecorator extends ZoneAvoidanceRule {
@Autowired
private PluginAdapter pluginAdapter;
private final Random random = new Random();
@Override
public Server choose(Object key) {
RuleEntity ruleEntity = pluginAdapter.getRule();
......@@ -112,7 +110,7 @@ public class ZoneAvoidanceRuleDecorator extends ZoneAvoidanceRule {
}
// 获取介于0(含)和n(不含)伪随机,均匀分布的int值
int hitWeight = random.nextInt(totalWeight) + 1; // [1, totalWeight)
int hitWeight = ThreadLocalRandom.current().nextInt(totalWeight) + 1; // [1, totalWeight)
for (int i = 0; i < weightHolder.length; i++) {
if (hitWeight <= weightHolder[i][1]) {
return weightHolder[i][0];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment