Skip to content

Commit 8a1d2cc

Browse files
authored
Issue #1487: reuse Random instance (#1488)
* Issue #1487: reuse Random instance * Issue #1487: reuse Random instance
1 parent 415f84a commit 8a1d2cc

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

changes.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
Changes log
22
===========
33

4-
- 2.6.1 (??-??-2025)
5-
4+
- 2.6.1 (??-??-2026)
5+
- Bugs fixed
6+
- Reuse an instance of Random class in RandomUtils. Issue #1487.
67
- 2.6.0 (29-06-2025)
78

89
- 2.6 Release Candidate 2 (21-06-2025)

org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Router.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public class Router extends Restlet {
9494

9595
/**
9696
* Each call will be randomly routed to one of the routes that reached the
97-
* required score. If the random route selected is not a match then the
97+
* required score. If the random route selected is not a match, then the
9898
* immediate next route is evaluated until one matching route is found. If we
9999
* get back to the initial random route selected with no match, then we return
100100
* null. Unless all the routes score above the required score, this mode will

org.restlet.java/org.restlet/src/main/java/org/restlet/util/RouteList.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
import org.restlet.Restlet;
1515
import org.restlet.routing.Route;
1616

17+
import java.security.SecureRandom;
1718
import java.util.Collections;
1819
import java.util.List;
19-
import java.util.Random;
2020
import java.util.concurrent.CopyOnWriteArrayList;
2121

2222
/**
@@ -34,14 +34,16 @@
3434
* @see java.util.List
3535
*/
3636
public final class RouteList extends WrapperList<Route> {
37-
/** The index of the last route used in the round robin mode. */
37+
/** The index of the last route used in the round-robin mode. */
3838
private volatile int lastIndex;
39+
/** Used when asked to return a random route. */
40+
private final SecureRandom random = new SecureRandom();
3941

4042
/**
4143
* Constructor.
4244
*/
4345
public RouteList() {
44-
super(new CopyOnWriteArrayList<Route>());
46+
super(new CopyOnWriteArrayList<>());
4547
this.lastIndex = -1;
4648
}
4749

@@ -51,7 +53,7 @@ public RouteList() {
5153
* @param delegate The delegate list.
5254
*/
5355
public RouteList(List<Route> delegate) {
54-
super(new CopyOnWriteArrayList<Route>(delegate));
56+
super(new CopyOnWriteArrayList<>(delegate));
5557
this.lastIndex = -1;
5658
}
5759

@@ -120,7 +122,7 @@ public synchronized Route getLast(Request request, Response response, float requ
120122
}
121123

122124
/**
123-
* Returns a next route match in a round robin mode for a given call.
125+
* Returns a next route match in a round-robin mode for a given call.
124126
*
125127
* @param request The request to score.
126128
* @param response The response to score.
@@ -159,7 +161,7 @@ public synchronized Route getRandom(Request request, Response response, float re
159161
int length = size();
160162

161163
if (length > 0) {
162-
int j = new Random().nextInt(length);
164+
int j = random.nextInt(length);
163165
Route route = get(j);
164166

165167
if (route.score(request, response) >= requiredScore) {
@@ -169,7 +171,7 @@ public synchronized Route getRandom(Request request, Response response, float re
169171
boolean loopedAround = false;
170172

171173
do {
172-
if ((j == length) && (loopedAround == false)) {
174+
if ((j == length) && !loopedAround) {
173175
j = 0;
174176
loopedAround = true;
175177
}
@@ -187,7 +189,7 @@ public synchronized Route getRandom(Request request, Response response, float re
187189
}
188190

189191
/**
190-
* Removes all routes routing to a given target.
192+
* Removes all routes to a given target.
191193
*
192194
* @param target The target Restlet to detach.
193195
*/

0 commit comments

Comments
 (0)