The ThreadLocalRandom
class in Java, part of the java.util.concurrent
package, is a specialized random number generator for use in concurrent applications. It is designed to reduce contention among threads by ensuring each thread has its own instance of the random number generator. This class provides better performance in multithreaded environments compared to the Random
class.
ThreadLocalRandom
ClassBefore using the ThreadLocalRandom
class, you need to import it:
import java.util.concurrent.ThreadLocalRandom;
Unlike Random
, you do not need to create an instance of ThreadLocalRandom
. Instead, you obtain the instance for the current thread using the current()
method:
ThreadLocalRandom random = ThreadLocalRandom.current();
To generate any random integer, use the nextInt()
method:
int randomInt = random.nextInt();
System.out.println("Random Integer: " + randomInt);
To generate a random integer within a specific range (e.g., from min
to max - 1
), use the nextInt(int origin, int bound)
method:
int randomIntInRange = random.nextInt(50, 100); // Generates a random integer between 50 and 99
System.out.println("Random Integer between 50 and 99: " + randomIntInRange);
To generate a random long value, use the nextLong()
method:
long randomLong = random.nextLong();
System.out.println("Random Long: " + randomLong);
To generate a random long value within a specific range, use the nextLong(long origin, long bound)
method:
long randomLongInRange = random.nextLong(100000L, 1000000L); // Generates a random long between 100000 and 999999
System.out.println("Random Long between 100000 and 999999: " + randomLongInRange);
To generate a random double value between 0.0 and 1.0, use the nextDouble()
method:
double randomDouble = random.nextDouble();
System.out.println("Random Double between 0.0 and 1.0: " + randomDouble);
To generate a random double value within a specific range, use the nextDouble(double origin, double bound)
method:
double randomDoubleInRange = random.nextDouble(5.5, 10.5); // Generates a random double between 5.5 and 10.5
System.out.println("Random Double between 5.5 and 10.5: " + randomDoubleInRange);
To generate a random boolean value, use the nextBoolean()
method:
boolean randomBoolean = random.nextBoolean();
System.out.println("Random Boolean: " + randomBoolean);
Here is a complete example demonstrating the use of ThreadLocalRandom
:
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomExample {
public static void main(String[] args) {
// Obtain the ThreadLocalRandom instance for the current thread
ThreadLocalRandom random = ThreadLocalRandom.current();
// Generate a random integer
int randomInt = random.nextInt();
System.out.println("Random Integer: " + randomInt);
// Generate a random integer between 50 and 99
int randomIntInRange = random.nextInt(50, 100);
System.out.println("Random Integer between 50 and 99: " + randomIntInRange);
// Generate a random long
long randomLong = random.nextLong();
System.out.println("Random Long: " + randomLong);
// Generate a random long between 100000 and 999999
long randomLongInRange = random.nextLong(100000L, 1000000L);
System.out.println("Random Long between 100000 and 999999: " + randomLongInRange);
// Generate a random double between 0.0 and 1.0
double randomDouble = random.nextDouble();
System.out.println("Random Double between 0.0 and 1.0: " + randomDouble);
// Generate a random double between 5.5 and 10.5
double randomDoubleInRange = random.nextDouble(5.5, 10.5);
System.out.println("Random Double between 5.5 and 10.5: " + randomDoubleInRange);
// Generate a random boolean
boolean randomBoolean = random.nextBoolean();
System.out.println("Random Boolean: " + randomBoolean);
}
}
The ThreadLocalRandom
class in Java provides a high-performance way to generate random numbers in concurrent applications. By using the current()
method, each thread gets its own instance of the random number generator, reducing contention and improving performance. This class supports generating random integers, longs, doubles, and booleans, both within specific ranges and as default values.