提交 034e1138 编写于 作者: M Mike Smith

Change RandomRule to use a ThreadLocalRandom instead of Random, as a perf optimization

上级 880b00d4
......@@ -17,11 +17,11 @@
*/
package com.netflix.loadbalancer;
import java.util.List;
import java.util.Random;
import com.netflix.client.config.IClientConfig;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
/**
* A loadbalacing strategy that randomly distributes traffic amongst existing
* servers.
......@@ -30,11 +30,6 @@ import com.netflix.client.config.IClientConfig;
*
*/
public class RandomRule extends AbstractLoadBalancerRule {
Random rand;
public RandomRule() {
rand = new Random();
}
/**
* Randomly choose from all living servers
......@@ -62,7 +57,7 @@ public class RandomRule extends AbstractLoadBalancerRule {
return null;
}
int index = rand.nextInt(serverCount);
int index = chooseRandomInt(serverCount);
server = upList.get(index);
if (server == null) {
......@@ -88,6 +83,10 @@ public class RandomRule extends AbstractLoadBalancerRule {
}
protected int chooseRandomInt(int serverCount) {
return ThreadLocalRandom.current().nextInt(serverCount);
}
@Override
public Server choose(Object key) {
return choose(getLoadBalancer(), key);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册