GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java ThreadLocalRandom - Complete Tutorial with Examples Java ThreadLocalRandom ClassLast modified: April 26, 2025
The
Random number generation is crucial in concurrent applications like games,
simulations, and load testing. ThreadLocalRandom Class Overview
Introduced in Java 7,
Accessible through the static method
In addition to its concurrency advantages, Basic Random Number Generation
This example demonstrates basic usage of BasicThreadLocalRandom.java
package com.zetcode;
import java.util.concurrent.ThreadLocalRandom;
public class BasicThreadLocalRandom {
public static void main(String[] args) {
ThreadLocalRandom random = ThreadLocalRandom.current();
// Generate random integer
int randInt = random.nextInt();
System.out.println("Random integer: " + randInt);
// Generate random integer between 0 and 100 (exclusive)
int randIntRange = random.nextInt(100);
System.out.println("Random integer (0-99): " + randIntRange);
// Generate random double between 0.0 and 1.0
double randDouble = random.nextDouble();
System.out.println("Random double: " + randDouble);
// Generate random long
long randLong = random.nextLong();
System.out.println("Random long: " + randLong);
}
}
This program uses Each run produces different results, as the generator is seeded uniquely per thread. The values are uniformly distributed, ideal for concurrent applications. Random Numbers in a Custom Range
This example shows how to generate random numbers within a specific range using
RandomRange.java
package com.zetcode;
import java.util.concurrent.ThreadLocalRandom;
public class RandomRange {
public static void main(String[] args) {
ThreadLocalRandom random = ThreadLocalRandom.current();
int min = 10;
int max = 20;
// Generate random integer in range [min, max)
int randInt = random.nextInt(min, max);
System.out.println("Random int (" + min + "-" + (max-1) + "): " + randInt);
// Generate random double in range [min, max)
double randDouble = random.nextDouble(min, max);
System.out.println("Random double (" + min + "-" + max + "): " + randDouble);
// Generate random long in range [min, max)
long randLong = random.nextLong(min, max);
System.out.println("Random long (" + min + "-" + (max-1) + "): " + randLong);
}
}
The program uses overloaded methods to generate numbers directly within the
specified range. The
This approach simplifies range generation compared to Concurrent Random Number Generation
This example illustrates ConcurrentRandom.java
package com.zetcode;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ConcurrentRandom {
public static void main(String[] args) {
try (ExecutorService executor = Executors.newFixedThreadPool(4)) {
for (int i = 0; i < 4; i++) {
executor.submit(() -> {
ThreadLocalRandom random = ThreadLocalRandom.current();
System.out.println(Thread.currentThread().getName() +
": " + random.nextInt(100));
});
}
executor.shutdown();
}
}
}
We create a thread pool and submit tasks that generate random numbers using
This demonstrates the class's strength in concurrent environments, where multiple threads can generate random numbers without synchronization overhead. Generating Random Streams
This example shows how to use RandomStreams.java
package com.zetcode;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
public class RandomStreams {
public static void main(String[] args) {
ThreadLocalRandom random = ThreadLocalRandom.current();
// Generate stream of random integers
System.out.println("Random integers (limit 5):");
IntStream intStream = random.ints(5);
intStream.forEach(System.out::println);
// Generate stream of random integers in range
System.out.println("\nRandom integers (50-100, limit 5):");
IntStream rangeStream = random.ints(5, 50, 100);
rangeStream.forEach(System.out::println);
// Generate stream of random doubles
System.out.println("\nRandom doubles (limit 5):");
random.doubles(5).forEach(System.out::println);
}
}
The program generates streams of random integers and doubles using
Streams provide a functional approach to random number generation, making
Random Selection from a List
This example demonstrates using RandomSelection.java
package com.zetcode;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class RandomSelection {
public static void main(String[] args) {
ThreadLocalRandom random = ThreadLocalRandom.current();
List<String> items = List.of("Apple", "Banana", "Orange", "Grape", "Mango");
// Select one random item
int index = random.nextInt(items.size());
String randomItem = items.get(index);
System.out.println("Random item: " + randomItem);
// Select multiple random items
System.out.println("Three random items:");
for (int i = 0; i < 3; i++) {
index = random.nextInt(items.size());
System.out.println(items.get(index));
}
}
}
We use
This technique is efficient in concurrent environments, as
Performance Comparison
This example compares the performance of PerformanceComparison.java
package com.zetcode;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class PerformanceComparison {
static final int COUNT = 1000000;
public static void main(String[] args) {
// Test Random
try (ExecutorService executor = Executors.newFixedThreadPool(4)) {
long start = System.currentTimeMillis();
for (int i = 0; i < 4; i++) {
executor.submit(() -> {
Random random = new Random();
for (int j = 0; j < COUNT; j++) {
random.nextInt();
}
});
}
executor.shutdown();
if (!executor.awaitTermination(1, TimeUnit.MINUTES)) {
System.err.println("Random tasks did not complete within the timeout.");
}
long duration = System.currentTimeMillis() - start;
System.out.println("Random time: " + duration + "ms");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println("Execution was interrupted.");
}
// Test ThreadLocalRandom
try (ExecutorService executor = Executors.newFixedThreadPool(4)) {
long start = System.currentTimeMillis();
for (int i = 0; i < 4; i++) {
executor.submit(() -> {
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int j = 0; j < COUNT; j++) {
random.nextInt();
}
});
}
executor.shutdown();
if (!executor.awaitTermination(1, TimeUnit.MINUTES)) {
System.err.println("ThreadLocalRandom tasks did not complete within the timeout.");
}
long duration = System.currentTimeMillis() - start;
System.out.println("ThreadLocalRandom time: " + duration + "ms");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println("Execution was interrupted.");
}
}
}
In this example, we use a fixed thread pool to execute tasks that generate
random numbers using both
The
Additionally, the use of Generating Random Arrays
This example shows how to use RandomArray.java
package com.zetcode;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
public class RandomArray {
public static void main(String[] args) {
ThreadLocalRandom random = ThreadLocalRandom.current();
int size = 10;
// Generate array of random integers
int[] intArray = new int[size];
for (int i = 0; i < size; i++) {
intArray[i] = random.nextInt(100);
}
System.out.println("Random integer array: " + Arrays.toString(intArray));
// Generate array of random doubles
double[] doubleArray = new double[size];
for (int i = 0; i < size; i++) {
doubleArray[i] = random.nextDouble();
}
System.out.println("Random double array: " + Arrays.toString(doubleArray));
}
}
The program creates arrays of random integers and doubles using
Such arrays are valuable for testing algorithms or simulating data in
multi-threaded applications, with output formatted using
SourceJava ThreadLocalRandom Documentation
This tutorial thoroughly explores the Java AuthorI am Jan Bodnar, a passionate programmer with extensive experience. Since 2007, I have written over 1,400 articles and eight e-books. With over eight years of teaching, I am dedicated to sharing knowledge and helping others learn programming concepts. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |