关于java:通俗易懂的JUC源码剖析ThreadLocalRandom

7次阅读

共计 3472 个字符,预计需要花费 9 分钟才能阅读完成。

一、为什么要用 ThreadLocalRandom?Random 不够用吗?

咱们对 Random 可能比拟相熟,随机数生成的罕用类。来回顾下 Random 的用法:

Random random = new Random();
// 输入一个 0~5 的随机数(包含 0,不包含 5)System.out.println(random.nextInt(5));

来看下 nextInt 的源码:

public int nextInt(int bound) {if (bound <= 0)
        throw new IllegalArgumentException(BadBound);
    // 1. 依据老的种子生成新的种子
    int r = next(31);
    int m = bound - 1;
    // 2. 依据新的种子计算随机数
    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;
}

再来看下依据老的种子生成新种子的代码:

protected int next(int bits) {
    long oldseed, nextseed;
    AtomicLong seed = this.seed;
    do {oldseed = seed.get();
        nextseed = (oldseed * multiplier + addend) & mask;
    } while (!seed.compareAndSet(oldseed, nextseed));
    return (int)(nextseed >>> (48 - bits));
}

能够看到,种子 seed 是用 AtomicLong 类型保留的,它是线程平安的,也就是说多个线程用 CAS 操作更新种子的时候,同一时刻只有一个线程更新胜利,其余线程会循环重试。这尽管保障了线程平安,但高并发时,会有大量线程在不停地自璇重试,这无疑升高了性能。所以,ThreadLocalRandom 应运而生。

二、ThreadLocalRandom 的实现原理

在探索实现原理前,先看看它的用法:

ThreadLocalRandom random = ThreadLocalRandom.current();
random.next(5);

先来看看它的类构造:

public class ThreadLocalRandom extends Random {}

能够看到,它继承了 Random,但跟 ThreadLocal 是怎么分割上的呢?
咱们能够猜测下,ThreadLocal 是多个线程领有本人的变量正本,那么 ThreadLocalRandom 是不是也是这个思路,多个线程都领有本人的 seed 种子变量呢? 这样就不必竞争同一个 seed,从而晋升性能。
往下持续看源码,来证实咱们的猜测:
先看下它的重要属性

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;
        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);
     }
}

能够看到,SEED、PROBE、SECONDARY 三个属性值都是 Thread 类里相应属性的偏移量。前面会剖析它们的作用。
再来看看 ThreadLocalRandom.curren()办法:

public static ThreadLocalRandom current() {if (UNSAFE.getInt(Thread.currentThread(), PROBE) == 0)
        // 初始化
        localInit();
    // instance 是饿汉式的单例
    // static final ThreadLocalRandom instance = new ThreadLocalRandom();
    return instance;
}

来看下 localInit()代码:

static final void localInit() {int p = probeGenerator.addAndGet(PROBE_INCREMENT);
    // 初始化 probe 值,跳过 0
    int probe = (p == 0) ? 1 : p; // skip 0
    // 初始 seed 值
    long seed = mix64(seeder.getAndAdd(SEEDER_INCREMENT));
    Thread t = Thread.currentThread();
    // 将 probe 和 seed 值设置到以后线程实例 t 中
    UNSAFE.putLong(t, SEED, seed);
    UNSAFE.putInt(t, PROBE, probe);
}

再来看看 nextInt()办法:

public int nextInt(int bound) {if (bound <= 0)
        throw new IllegalArgumentException(BadBound);
    // 1. 依据老种子计算新种子    
    int r = mix32(nextSeed());
    // 2. 依据新种子计算随机数
    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;
}

能够看到,上述步骤和 Random 类似,要害在 nextSeed()外面:

final long nextSeed() {
    Thread t; long r; // read and update per-thread seed
    // 获取以后线程 t 的 seed 旧值,减少 GAMMA 值后,批改回以后线程 t,返回新种子的值 r
    UNSAFE.putLong(t = Thread.currentThread(), SEED,
    r = UNSAFE.getLong(t, SEED) + GAMMA);
    return r;
}

能够看到,每个线程操作的种子都是本人线程绑定的 threadLocalRandomSeed,不会和其余线程产生竞争,因而晋升了性能。

三、总结

最初提下 SEED、PROBE、SECONDARY 三个属性值的作用。
Thread 类外面有这 3 个属性的简略正文:

/** 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;

SEED 很显然:就是本文说的随机数种子。
PROBE:非 0 的 long 类型值。翻译过去是线程探针,在本文如同没有施展重要作用,然而在其余类外面,比方 LongAdder、ConcurrentHashMap 外面都会用到这个 probe,这个探针的作用是哈希线程,将线程和数组中的不同元素对应起来,尽量避免线程争用同一数组元素。能够翻看我的另一篇文章对于 LongAdder 的源码剖析。
SECONDARY:翻译过去是第二种子,在 ConcurrentSkipListMap 外面
会用到,之后的文章外面会剖析它的作用,请继续关注 ^_^

正文完
 0