ThreadLocalRandom vs Random

java

ThreadLocalRandom was introduced since java 7 as a faster Random generator than Random. According to Effective Java, “Random should no longer be used as ThreadLocalRandom is faster and produce higher quality random numbers”. Why?

Share RNG among threads:

import java.util.concurrent.ThreadLocalRandom;
import java.util.Random;

public class ThreadLocalRandomTest {
  static ThreadLocalRandom r1 = ThreadLocalRandom.current();
  static ThreadLocalRandom r2;
  static Random random = new Random();
  public static void main(String ...args) {
    new Thread(() -> {
      r2 = ThreadLocalRandom.current();
      System.out.println(r1 == r2); // true, singleton
    }).start();
    for (int i=0 ; i<4; i++) {
      new Thread(() -> {
        // cannot call r1.nextInt() otherwise all threads print the same number
        System.out.println("ThreadLocalRandom " + ThreadLocalRandom.current().nextInt());
        System.out.println("Random " + random.nextInt());
      }).start();
    }
  }
}

Both class are pseudo generators, same seed produces same number. So to generate different number in different thread, the seed in each thread should be different.

Regarding Random, same Random instance is shared by all threads. All spins to CAS the seed. Only one of them wins at the same time.

random.nextInt()

However, ThreadLocalRandom is a singleton. It provides a static factory method ThreadLocalRandom.current() to create instance. The method initializes the prob and seed in current Thread. As a result, they don't need to contend for the same seed as everyone has a private one.

ThreadLocalRandom.current()

nextSeed

nextSeed() is as simple as Thread.seed = seed + gamma.

Less contention on shared resource, that’s why LocalThreadRandom is faster. But how about new a Random instance in every thread? Although there’s no contention on seed but it cost more space as a new random instance is created in every thread and CAS operation has more overhead than ordinary get and set from the viewpoint of memory consistency.