[JAVA-4645] Investigate ThreadLocalRandom interaction with virtual threads Created: 13/Jun/22  Updated: 07/Sep/23  Resolved: 07/Sep/23

Status: Closed
Project: Java Driver
Component/s: Internal
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Unknown
Reporter: Jeffrey Yemin Assignee: Unassigned
Resolution: Done Votes: 0
Labels: loom
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: Virtual Threads Support
Quarter: FY24Q3
Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

The driver uses ThreadLocalRandom in a couple of places. It's not clear how ThreadLocalRandom will work in a world with millions of virtual threads. It appears that in Java 19 ThreadLocalRandom#localInit will be called on every invocation of ThreadLocalRandom#current, which seems undesirable.



 Comments   
Comment by Jeffrey Yemin [ 20/Mar/23 ]

I looked into this a bit more and it seems ThreadLocalRandom#localInit is not very expensive in time or space. It's essentially one CAS operation and a bit of constant-time arithmetic, resulting ultimately in the initialization of a couple of integers in the Thread instance.

Comment by Maxim Katcharov [ 22/Jun/22 ]

If this is truly a significant problem, we might consider rolling our own PRNG/seeding that is weaker than ThreadLocalRandom but still strong enough for our purposes. For example, we might seed Random using (thread_id xor time).

Generated at Thu Feb 08 09:02:36 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.