Skip to content

Commit 970f56e

Browse files
authored
Use ThreadLocalRandom for thread selection in OrderedExecutor (#4832)
1 parent f1d7cc1 commit 970f56e

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

bookkeeper-common/src/main/java/org/apache/bookkeeper/common/util/OrderedExecutor.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
import java.util.Collection;
2929
import java.util.List;
3030
import java.util.Map;
31-
import java.util.Random;
3231
import java.util.concurrent.Callable;
3332
import java.util.concurrent.ExecutionException;
3433
import java.util.concurrent.ExecutorService;
3534
import java.util.concurrent.Future;
3635
import java.util.concurrent.ThreadFactory;
36+
import java.util.concurrent.ThreadLocalRandom;
3737
import java.util.concurrent.ThreadPoolExecutor;
3838
import java.util.concurrent.TimeUnit;
3939
import java.util.concurrent.TimeoutException;
@@ -65,7 +65,6 @@ public class OrderedExecutor implements ExecutorService {
6565
final String name;
6666
final ExecutorService[] threads;
6767
final long[] threadIds;
68-
final Random rand = new Random();
6968
final OpStatsLogger taskExecutionStats;
7069
final OpStatsLogger taskPendingStats;
7170
final boolean traceTaskExecution;
@@ -545,7 +544,7 @@ public ExecutorService chooseThread() {
545544
return threads[0];
546545
}
547546

548-
return threads[rand.nextInt(threads.length)];
547+
return threads[ThreadLocalRandom.current().nextInt(threads.length)];
549548
}
550549

551550
public ExecutorService chooseThread(Object orderingKey) {
@@ -555,7 +554,7 @@ public ExecutorService chooseThread(Object orderingKey) {
555554
}
556555

557556
if (null == orderingKey) {
558-
return threads[rand.nextInt(threads.length)];
557+
return threads[ThreadLocalRandom.current().nextInt(threads.length)];
559558
} else {
560559
return threads[chooseThreadIdx(orderingKey.hashCode(), threads.length)];
561560
}

0 commit comments

Comments
 (0)