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

  1. ThreadLocalRandom provide more good performance and less overhead in in concurrent programs than the shared Random object. So, the usage of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask) use random numbers in parallel in thread pools.
  2. ThreadLocalRandom does not support the explicit setting of seed for more true randomness. Also, If we try to override setSeed(long) method, an UnsupportedOperationException will be thrown because the ThreadLocalRandom prohibits explicit setting of its seed by overriding Random’s setSeed(long) method and automatically throwing an UnsupportedOperationException 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.


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)

		// 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)

		// 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)

		// Returns a pseudorandom, uniformly distributed long value between 5000
		// (inclusive) and 10000 (exclusive)
		System.out.println(ThreadLocalRandom.current().nextDouble(5000, 10000));



Random int:
Random long:
Random double:

Download the Source Code of this example:

This was an example of Java ThreadLocalRandom class.

You can download the full source code of this example here:

Ashraf Sarhan

Ashraf Sarhan is a passionate software engineer, an open source enthusiast, has a Bsc. degree in Computer and Information Systems from Alexandria University. He is experienced in building large, scalable and distributed enterprise applications/service in multiple domains. He also has a keen interest in JavaEE, SOA, Agile and Big Data technologies.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button