ThreadLocalRandom
java.util.concurrent.ThreadLocalRandom Example
In this example we shall show you how to make use of ThreadLocalRandom
class, ThreadLocalRandom
is a random number generator like its parent Random
class. Like the global Random
generator class, a ThreadLocalRandom
is initialized with an internally generated seed that may not otherwise be modified.
1. ThreadLocalRandom Advantages
ThreadLocalRandom
provide more good performance and less overhead in in concurrent programs than the sharedRandom
object. So, the usage ofThreadLocalRandom
is particularly appropriate when multiple tasks (for example, each aForkJoinTask
) use random numbers in parallel in thread pools.ThreadLocalRandom
does not support the explicit setting of seed for more true randomness. Also, If we try to overridesetSeed(long)
method, anUnsupportedOperationException
will be thrown because theThreadLocalRandom
prohibits explicit setting of its seed by overriding Random’ssetSeed(long)
method and automatically throwing anUnsupportedOperationException
if called.
2. Example
Usages of this class should typically be of the form: ThreadLocalRandom.current().nextX(...)
(where X is int
, long
, etc). When all usages are of this form, it is never possible to accidentally share a ThreadLocalRandom
across multiple threads.
ThreadLocalRandomTest:
package com.jcg; import java.util.concurrent.ThreadLocalRandom; /** * @author ashraf * */ public class ThreadLocalRandomTest { public static void main(String[] args) { System.out.println("Random int:"); // Returns a pseudorandom, uniformly distributed integer value between 0 // (inclusive) and 10000 (exclusive) System.out.println(ThreadLocalRandom.current().nextInt(10000)); // Returns a pseudorandom, uniformly distributed int value between 5000 // (inclusive) and 10000 (exclusive) System.out.println(ThreadLocalRandom.current().nextInt(5000, 10000)); System.out.println("Random long:"); // Returns a pseudorandom, uniformly distributed long value between 0 // (inclusive) and 10000 (exclusive) System.out.println(ThreadLocalRandom.current().nextLong(10000)); // Returns a pseudorandom, uniformly distributed long value between 5000 // (inclusive) and 10000 (exclusive) System.out.println(ThreadLocalRandom.current().nextLong(5000, 10000)); System.out.println("Random double:"); // Returns a pseudorandom, uniformly distributed long value between 0 // (inclusive) and 10000 (exclusive) System.out.println(ThreadLocalRandom.current().nextDouble(10000)); // Returns a pseudorandom, uniformly distributed long value between 5000 // (inclusive) and 10000 (exclusive) System.out.println(ThreadLocalRandom.current().nextDouble(5000, 10000)); } }
Output:
Random int: 8821 6475 Random long: 9739 5238 Random double: 2813.5399468694077 8616.95174974552
Download the Source Code of this example:
This was an example of Java ThreadLocalRandom
class.
Download
You can download the full source code of this example here: ThreadLocalRandomExampleCode.zip
You can download the full source code of this example here: ThreadLocalRandomExampleCode.zip