-
-
Notifications
You must be signed in to change notification settings - Fork 15.8k
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
Use ThreadLocalRandom instead of Math.random() #11165
Conversation
@@ -178,7 +179,7 @@ protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object | |||
|
|||
// Write payload | |||
if (maskPayload) { | |||
int random = (int) (Math.random() * Integer.MAX_VALUE); | |||
int random = PlatformDependent.threadLocalRandom().nextInt(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was previously never negative but now it can be. That won't be a problem, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I know it's not a problem. But that's a good point. Who knows, maybe someone already expects it to be positive :). So let's preserve the prev behavior just in case.
Motivation: `ThreadLocalRandom` doesn't cause contention. Also `nextInt()` generates only 4 random bytes while `Math.random()` generates 8 bytes. Modification: Replaced `(int) Math.random()` with `PlatformDependent.threadLocalRandom().nextInt()` Result: No possible contention when random numbers for WebSockets.
@doom369 Thanks! |
Motivation: `ThreadLocalRandom` doesn't cause contention. Also `nextInt()` generates only 4 random bytes while `Math.random()` generates 8 bytes. Modification: Replaced `(int) Math.random()` with `PlatformDependent.threadLocalRandom().nextInt()` Result: No possible contention when random numbers for WebSockets.
Motivation:
ThreadLocalRandom doesn't cause contention. Also
nextInt()
generates only 4 random bytes while Math.random() generates 8 bytes.Modification:
Replaced
(int) Math.random()
withPlatformDependent.threadLocalRandom().nextInt()
Result:
No possible contention when random numbers for WebSockets.