8000 opt: replace Random with ThreadLocalRandom by funky-eyes · Pull Request #373 · sofastack/sofa-bolt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

opt: replace Random with ThreadLocalRandom< 8000 /bdi> #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/java/com/alipay/remoting/RandomSelectStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import com.alipay.remoting.config.BoltClientOption;
import com.alipay.remoting.config.Configuration;
Expand All @@ -39,7 +39,6 @@ public class RandomSelectStrategy implements ConnectionSelectStrategy {
private static final Logger logger = BoltLoggerFactory.getLogger("CommonDefault");

private static final int MAX_TIMES = 5;
private final Random random = new Random();
private final Configuration configuration;

public RandomSelectStrategy(Configuration configuration) {
Expand Down Expand Up @@ -96,7 +95,7 @@ private Connection randomGet(List<Connection> connections) {
int tries = 0;
Connection result = null;
while ((result == null || !result.isFine()) && tries++ < MAX_TIMES) {
result = connections.get(this.random.nextInt(size));
result = connections.get(ThreadLocalRandom.current().nextInt(size));
}

if (result != null && !result.isFine()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;

import org.slf4j.Logger;

Expand All @@ -45,11 +45,9 @@ public class ScheduledDisconnectStrategy implements ConnectionMonitorStrategy {
private static final Logger logger = BoltLoggerFactory.getLogger("CommonDefault");

private final int connectionThreshold;
private final Random random;

public ScheduledDisconnectStrategy() {
this.connectionThreshold = ConfigManager.conn_threshold();
this.random = new Random();
}

/**
Expand Down Expand Up @@ -103,8 +101,8 @@ public void monitor(Map<String, RunStateRecordedFutureTask<ConnectionPool>> conn
}

if (serviceOnConnections.size() > connectionThreshold) {
Connection freshSelectConnect = serviceOnConnections.get(random
.nextInt(serviceOnConnections.size()));
Connection freshSelectConnect = serviceOnConnections.get(ThreadLocalRandom
.current().nextInt(serviceOnConnections.size()));
freshSelectConnect.setAttribute(Configs.CONN_SERVICE_STATUS,
Configs.CONN_SERVICE_STATUS_OFF);
serviceOffConnections.add(freshSelectConnect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicLong;

import com.alipay.remoting.config.BoltClientOption;
Expand Down Expand Up @@ -60,7 +60,7 @@ public class BenchmarkClient {
private static final byte[] BYTES = new byte[128];

static {
new Random().nextBytes(BYTES);
ThreadLocalRandom.current().nextBytes(BYTES);
}

public static void main(String[] args) throws RemotingException, InterruptedException {
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/com/alipay/remoting/rpc/common/RequestBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.alipay.remoting.rpc.common;

import java.io.Serializable;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
* biz request as a demo
Expand Down Expand Up @@ -49,8 +49,6 @@ public class RequestBody implements Serializable {
/** body */
private byte[] body;

private Random r = new Random();

public RequestBody() {
//json serializer need default constructor
}
Expand All @@ -64,7 +62,7 @@ public RequestBody(int id, int size) {
this.id = id;
this.msg = "";
this.body = new byte[size];
r.nextBytes(this.body);
ThreadLocalRandom.current().nextBytes(this.body);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package com.alipay.remoting.rpc.userprocessor.multiinterestprocessor;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
* @antuor muyun.cyt (muyun.cyt@antfin.com) 2018/7/5 11:20 AM
Expand Down Expand Up @@ -44,8 +44,6 @@ public class RequestBodyC1 implements MultiInterestBaseRequestBody {
/** body */
private byte[] body;

private Random r = new Random();

public RequestBodyC1() {
//json serializer need default constructor
}
Expand All @@ -59,7 +57,7 @@ public RequestBodyC1(int id, int size) {
this.id = id;
this.msg = "";
this.body = new byte[size];
r.nextBytes(this.body);
ThreadLocalRandom.current().nextBytes(this.body);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package com.alipay.remoting.rpc.userprocessor.multiinterestprocessor;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
* @antuor muyun.cyt (muyun.cyt@antfin.com) 2018/7/5 11:20 AM
Expand Down Expand Up @@ -44,8 +44,6 @@ public class RequestBodyC2 implements MultiInterestBaseRequestBody {
/** body */
private byte[] body;

private Random r = new Random();

public RequestBodyC2() {
//json serializer need default constructor
//super();
Expand All @@ -62,7 +60,7 @@ public RequestBodyC2(int id, int size) {
this.id = id;
this.msg = "";
this.body = new byte[size];
r.nextBytes(this.body);
ThreadLocalRandom.current().nextBytes(this.body);
}

/**
Expand Down
Loading
0