You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by yi...@apache.org on 2018/09/05 03:31:56 UTC
[incubator-dubbo] branch master updated: replace Random with
ThreadLocalRandom (#2433)
This is an automated email from the ASF dual-hosted git repository.
yiji pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 3bdc316 replace Random with ThreadLocalRandom (#2433)
3bdc316 is described below
commit 3bdc316511f9b5e2093875f7ded3f7da791edab7
Author: xujingfeng <25...@qq.com>
AuthorDate: Wed Sep 5 11:31:52 2018 +0800
replace Random with ThreadLocalRandom (#2433)
---
.../dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java | 8 +++-----
.../apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java | 8 +++-----
2 files changed, 6 insertions(+), 10 deletions(-)
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java
index 7a8eb3d..fd83cf2 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java
@@ -23,7 +23,7 @@ import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcStatus;
import java.util.List;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
/**
* LeastActiveLoadBalance
@@ -33,8 +33,6 @@ public class LeastActiveLoadBalance extends AbstractLoadBalance {
public static final String NAME = "leastactive";
- private final Random random = new Random();
-
@Override
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
int length = invokers.size(); // Number of invokers
@@ -72,7 +70,7 @@ public class LeastActiveLoadBalance extends AbstractLoadBalance {
}
if (!sameWeight && totalWeight > 0) {
// If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight.
- int offsetWeight = random.nextInt(totalWeight);
+ int offsetWeight = ThreadLocalRandom.current().nextInt(totalWeight);
// Return a invoker based on the random value.
for (int i = 0; i < leastCount; i++) {
int leastIndex = leastIndexs[i];
@@ -82,6 +80,6 @@ public class LeastActiveLoadBalance extends AbstractLoadBalance {
}
}
// If all invokers have the same weight value or totalWeight=0, return evenly.
- return invokers.get(leastIndexs[random.nextInt(leastCount)]);
+ return invokers.get(leastIndexs[ThreadLocalRandom.current().nextInt(leastCount)]);
}
}
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java
index b5dea51..90bc60c 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java
@@ -21,7 +21,7 @@ import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import java.util.List;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
/**
* random load balance.
@@ -31,8 +31,6 @@ public class RandomLoadBalance extends AbstractLoadBalance {
public static final String NAME = "random";
- private final Random random = new Random();
-
@Override
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
int length = invokers.size(); // Number of invokers
@@ -48,7 +46,7 @@ public class RandomLoadBalance extends AbstractLoadBalance {
}
if (totalWeight > 0 && !sameWeight) {
// If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight.
- int offset = random.nextInt(totalWeight);
+ int offset = ThreadLocalRandom.current().nextInt(totalWeight);
// Return a invoker based on the random value.
for (int i = 0; i < length; i++) {
offset -= getWeight(invokers.get(i), invocation);
@@ -58,7 +56,7 @@ public class RandomLoadBalance extends AbstractLoadBalance {
}
}
// If all invokers have the same weight value or totalWeight=0, return evenly.
- return invokers.get(random.nextInt(length));
+ return invokers.get(ThreadLocalRandom.current().nextInt(length));
}
}