I'm looking to use (seeded) Random
objects across multiple threads, and the javadocs pointed me to ThreadLocalRandom
which looks great except I can't set the seed, so I can't ensure consistency among different threads or runs. Is there any practical reason to use ThreadLocalRandom
or would it be acceptable to do something like the following:
// Pass returned ThreadLocal object to all threads which need it
public static ThreadLocal<Random> threadRandom(final long seed) {
return new ThreadLocal<Random>(){
@Override
protected Random initialValue() {
return new Random(seed);
}
};
}
Using ThreadLocalRandom in concurrent programs will typically encounter much less overhead and contention than using the global Random class. You can use ThreadLocalRandom when multiple tasks like a ForkJoinTask need random numbers in parallel in thread pools.
As described in its javadoc, ThreadLocalRandom is similar to Random (i.e. not secure) but with better performance in case of concurrent access. Instances of ThreadLocalRandom are not cryptographically secure. Consider instead using SecureRandom in security-sensitive applications.
random() guarantees it's safe for use by multiple threads. But the Random class does not.
int boundedRandomValue = ThreadLocalRandom. current(). nextInt(0, 100); Please note, 0 is the inclusive lower limit and 100 is the exclusive upper limit.
You can simply use Random
, just make sure each Random
object is only accessed within a single thread.
Random
, being an ancient class like Vector
, is unnecessarily heavily synchronized. They probably wanted to show off Java's threading support since it was a big deal at that time. Also Java was mostly intended to run on consumer PCs which mostly had a single processor so synchronization didn't impact scaling like it does today on multiprocessors.
Now an obvious answer is to provide a thread-unsafe version of Random
, just like providing the thread-unsfae ArrayList
as the alternative to Vector
. That didn't happen, instead, we got ThreadLocalRandom
. That is kind of odd, not sure what's the motivation behind that. In java8, ThreadLocalRandom
is further optimized to operate directly on some int fields in the Thread
object.
The code for ThreadLocalRandom appears to be implemented as a ThreadLocal
anyway (not exactly like you've put it, but probably close enough). I think what you have will work great.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With