public class MyTest5 { public static void main(String[] args) { Random random = new Random(); IntStream.range(0,10).forEach(i -> { System.out.println(random.nextInt(10)); }); } }
public class Random implements java.io.Serializable { private final AtomicLong seed;
public int nextInt(int bound) { if (bound <= 0) throw new IllegalArgumentException(BadBound);
int r = next(31); int m = bound - 1; if ((bound & m) == 0) // i.e., bound is a power of 2 r = (int)((bound * (long)r) >> 31); else { for (int u = r; u - (r = u % bound) + m < 0; u = next(31)) ; } return r; } }
public class MyTest5 { public static void main(String[] args) { ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current(); IntStream.range(0,10).forEach(i -> { System.out.println(threadLocalRandom.nextInt(10)); }); }
A random number generator isolated to the current thread. Like the global Random generator used by the Math class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified. When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention. Use of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask) use random numbers in parallel in thread pools. 一个线程隔离的随机数生成器。就像全局的生成器一样被Math类所使用的。ThreadLocalRandom也是通过内部的种子生成器来初始化的,但是不能被其他的线程修改,更多的可能下,在并发的情况下,使用ThreadLocalRandom比共享的Random,特别是在ForkJoinTask这种情况当中,会表现出很好的性能。
public class ThreadLocalRandom extends Random { ... // 在同一个jvm 应用里边只有一个ThreadLocalRandom实例 static final ThreadLocalRandom instance = new ThreadLocalRandom(); public static ThreadLocalRandom current() { if (UNSAFE.getInt(Thread.currentThread(), PROBE) == 0) localInit(); return instance; }
public int nextInt(int bound) { if (bound <= 0) throw new IllegalArgumentException(BadBound); int r = mix32(nextSeed()); int m = bound - 1; if ((bound & m) == 0) // power of two r &= m; else { // reject over-represented candidates for (int u = r >>> 1; u + m - (r = u % bound) < 0; u = mix32(nextSeed()) >>> 1) ; } return r; }
final long nextSeed() { Thread t; long r; // read and update per-thread seed //对当前线程进行操作更新 UNSAFE.putLong(t = Thread.currentThread(), SEED, r = UNSAFE.getLong(t, SEED) + GAMMA); return r; }
// Unsafe mechanics private static final sun.misc.Unsafe UNSAFE; private static final long SEED; private static final long PROBE; private static final long SECONDARY; static { try { UNSAFE = sun.misc.Unsafe.getUnsafe(); Class<?> tk = Thread.class; //对种子的更新是修改的Thread类里边的成员变量 SEED = UNSAFE.objectFieldOffset (tk.getDeclaredField("threadLocalRandomSeed")); PROBE = UNSAFE.objectFieldOffset (tk.getDeclaredField("threadLocalRandomProbe")); SECONDARY = UNSAFE.objectFieldOffset (tk.getDeclaredField("threadLocalRandomSecondarySeed")); } catch (Exception e) { throw new Error(e); } }
... }
Thread类的部分代码:
1 2 3 4 5 6 7 8 9 10 11
/** The current seed for a ThreadLocalRandom */ @sun.misc.Contended("tlr") long threadLocalRandomSeed;
/** Probe hash value; nonzero if threadLocalRandomSeed initialized */ @sun.misc.Contended("tlr") int threadLocalRandomProbe;
/** Secondary seed isolated from public ThreadLocalRandom sequence */ @sun.misc.Contended("tlr") int threadLocalRandomSecondarySeed;