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

  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.

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

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.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button