Close

ThreadLocalRandom Example

[Last Updated: Feb 20, 2016]

Java Java 8 Streams Java Concurrency 

For concurrent access, using ThreadLocalRandom instead of Math.random() results in less contention and, ultimately, better performance.

This class was introduced in JDK 1.7

Examples

int i = ThreadLocalRandom.current()
.nextInt(100, 1000);


Using streams

IntStream ints = ThreadLocalRandom.current()
.ints(100, 1000)
.limit(1000)
.forEach(System.out::println);

See Also