ThreadLocalRandom#getProbe #advanceProbe浅析

前言

Briefly, a thread’s “probe” value is a non-zero hash code that (probably) does not collide with other existing threads with respect to any power of two collision space. When it does collide, it is pseudo-randomly adjusted (using a Marsaglia XorShift).

简单的说,一个线程的probe探针值,是一个非零hash值,它不会和其他线程重复(一定情况下)。

但是不重复的情况指的是在int的范围内不重复( − 2 31 ∼ 2 31 − 1 -2^{31}\sim2^{31}-1 2312311),在有限的桶位面前,由于只取probe值的后几位bit,那么很大概率也会重复。

单线程下使用

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.ThreadLocalRandom;

public class test4 {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        ThreadLocalRandom a = ThreadLocalRandom.current();

        Class<ThreadLocalRandom> clazz = ThreadLocalRandom.class;
        Method getMethod = null;
        Method advanceMethod = null;
        try {
            getMethod = clazz.getDeclaredMethod("getProbe");
            advanceMethod = clazz.getDeclaredMethod("advanceProbe", int.class);
            getMethod.setAccessible(true);
            advanceMethod.setAccessible(true);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        int h;
        System.out.println(h = (Integer) getMethod.invoke(a));
        System.out.println(getMethod.invoke(a));
        System.out.println(getMethod.invoke(a));

        System.out.println(h = (Integer) advanceMethod.invoke(a, h));
        System.out.println(getMethod.invoke(a));
        System.out.println(getMethod.invoke(a));

        System.out.println(h = (Integer) advanceMethod.invoke(a, h));
        System.out.println(getMethod.invoke(a));
        System.out.println(getMethod.invoke(a));
    }
}
-1640531527
-1640531527
-1640531527
1359758873
1359758873
1359758873
-533834434
-533834434
-533834434

由于两个方法不是public,需要通过反射才能调用。

  • 多次运行也一定是上面的运行结果,说明第一个线程拿到的probe种子是固定的。
  • getProbe获得当前的探针值,如果从来没有调用过advanceProbe,那么获得的是,probe序列的第一个值(即probe种子)。
  • advanceProbe根据旧probe值生成新的probe值。

多线程下使用

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.ThreadLocalRandom;

public class test3 {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        Class<ThreadLocalRandom> clazz = ThreadLocalRandom.class;
        Method getMethod = null;
        Method advanceMethod = null;
        try {
            getMethod = clazz.getDeclaredMethod("getProbe");
            advanceMethod = clazz.getDeclaredMethod("advanceProbe", int.class);
            getMethod.setAccessible(true);
            advanceMethod.setAccessible(true);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        for(int i = 0; i < 5; i++) {
            Method finalGetMethod = getMethod;
            Method finalAdvanceMethod = advanceMethod;
            new Thread(()->{
                ThreadLocalRandom a = ThreadLocalRandom.current();
                int h;
                try{
                    System.out.println(Thread.currentThread().getName()+" "+(h = (Integer) finalGetMethod.invoke(a)));

                    System.out.println(Thread.currentThread().getName()+" "+(h = (Integer) finalAdvanceMethod.invoke(a, h)));

                    System.out.println(Thread.currentThread().getName()+" "+(h = (Integer) finalAdvanceMethod.invoke(a, h)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }

    }
}
Thread-4 1013904242
Thread-0 -1640531527
Thread-1 2027808484
Thread-1 1143052388
Thread-1 -1836491342
Thread-3 -626627285
Thread-2 387276957
Thread-3 -144452630
Thread-0 1359758873
Thread-4 -1575449550
Thread-4 -1068750243
Thread-0 -533834434
Thread-3 784628165
Thread-2 -606781730
Thread-2 -36250631

发现多线程下使用,每个线程产生的probe序列,都没有重复的。

多线程是否冲突

创建threadNum个线程,每个线程获得probeTimes次探针值,将每个线程获得过的探针值都放入同一个集合中,最后判断集合大小是否和threadNum * probeTimes相等。

import java.lang.reflect.Method;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;

public class test3 {
    public static void main(String[] args) throws InterruptedException {
        Class<ThreadLocalRandom> clazz = ThreadLocalRandom.class;
        Method getMethod = null;
        Method advanceMethod = null;
        try {
            getMethod = clazz.getDeclaredMethod("getProbe");
            advanceMethod = clazz.getDeclaredMethod("advanceProbe", int.class);
            getMethod.setAccessible(true);
            advanceMethod.setAccessible(true);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        int threadNum = 5;
        int probeTimes = 100000;
        Thread[] ThreadArray = new Thread[threadNum];
        Set<Integer> s = ConcurrentHashMap.<Integer> newKeySet();

        for(int i = 0; i < threadNum; i++) {
            Method finalGetMethod = getMethod;
            Method finalAdvanceMethod = advanceMethod;
            ThreadArray[i] = new Thread(()->{
                ThreadLocalRandom a = ThreadLocalRandom.current();
                int h = 0;
                try{
                    for(int j = 0;j <probeTimes; j++) {
                        if (j == 0) {//第一次去get
                            h = (Integer) finalGetMethod.invoke(a);
                        } else {//之后每次都增加
                            h = (Integer) finalAdvanceMethod.invoke(a, h);
                        }
                        s.add(h);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            ThreadArray[i].start();
        }

        for(int i = 0; i < threadNum; i++) {
            ThreadArray[i].join();
        }
        System.out.println("探针生成的总次数:"+threadNum * probeTimes);
        System.out.println("不重复的探针数:"+s.size());
    }
}
  • 当threadNum和probeTimes都比较小时,探针总是不重复的。
        int threadNum = 10000;
        int probeTimes = 82;
  • 当threadNum和probeTimes为上面的值时,探针开始重复。
  • ThreadLocalRandom的probe机制在线程数比较少,且probe移动次数也比较小时,是可以保证探针不重复的。

ThreadLocalRandom.current()的使用

    /** The common ThreadLocalRandom */
    static final ThreadLocalRandom instance = new ThreadLocalRandom();

不管有多少个线程来调用ThreadLocalRandom.current(),返回的都是同一个实例,因为它是一个单例。

    public static ThreadLocalRandom current() {
        if (UNSAFE.getInt(Thread.currentThread(), PROBE) == 0)
            localInit();
        return instance;
    }

但即使这样,也应该让每个线程分别调用ThreadLocalRandom.current(),为的是让每个线程分别调用localInit。因为localInit里会对当前线程Thread初始化设置threadLocalRandomSeed值和threadLocalRandomProbe值。

    static final void localInit() {
        int p = probeGenerator.addAndGet(PROBE_INCREMENT);
        int probe = (p == 0) ? 1 : p; // skip 0
        long seed = mix64(seeder.getAndAdd(SEEDER_INCREMENT));
        Thread t = Thread.currentThread();
        UNSAFE.putLong(t, SEED, seed);//初始化设置
        UNSAFE.putInt(t, PROBE, probe);//初始化设置
    }

    private static final long SEED;
    private static final long PROBE;
    static {
        try {
            UNSAFE = sun.misc.Unsafe.getUnsafe();
            Class<?> tk = Thread.class;
            SEED = UNSAFE.objectFieldOffset
                (tk.getDeclaredField("threadLocalRandomSeed"));
            PROBE = UNSAFE.objectFieldOffset
                (tk.getDeclaredField("threadLocalRandomProbe"));
        } catch (Exception e) {
            throw new Error(e);
        }
    }
//Thread.java
    /** 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;

probe种子

    private static final AtomicInteger probeGenerator =
        new AtomicInteger();

    private static final int PROBE_INCREMENT = 0x9e3779b9;

每个线程通过int p = probeGenerator.addAndGet(PROBE_INCREMENT);获得probe序列的种子,每个线程肯定获得的都不一样,因为每个线程都累加了PROBE_INCREMENT

其他

没有调用过ThreadLocalRandom.current()ThreadLocalRandom.getProbe()获得的肯定是0(对象默认的初始化)。

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ThreadLocalJava 中的一个类,它提供了一种线程局部变量的机制。线程局部变量是指每个线程都有自己的变量副本,每个线程对该变量的访问都是独立的,互不影响。 ThreadLocal 主要用于解决多线程并发访问共享变量时的线程安全问题。在多线程环境下,如果多个线程共同访问同一个变量,可能会出现竞争条件,导致数据不一致或者出现线程安全问题。通过使用 ThreadLocal,可以为每个线程提供独立的副本,从而避免了线程安全问题。 ThreadLocal 的工作原理是,每个 Thread 对象内部都维护了一个 ThreadLocalMap 对象,ThreadLocalMap 是一个 key-value 结构,其中 key 是 ThreadLocal 对象,value 是该线程对应的变量副本。当访问 ThreadLocal 的 get() 方法时,会根据当前线程获取到对应的 ThreadLocalMap 对象,并从中查找到与 ThreadLocal 对象对应的值。如果当前线程尚未设置该 ThreadLocal 对象的值,则会通过 initialValue() 方法初始化一个值,并将其存入 ThreadLocalMap 中。当访问 ThreadLocal 的 set() 方法时,会将指定的值存入当前线程对应的 ThreadLocalMap 中。 需要注意的是,ThreadLocal 并不能解决共享资源的并发访问问题,它只是提供了一种线程内部的隔离机制。在使用 ThreadLocal 时,需要注意合理地使用,避免出现内存泄漏或者数据不一致的情况。另外,由于 ThreadLocal 使用了线程的 ThreadLocalMap,因此在使用完 ThreadLocal 后,需要手动调用 remove() 方法清理对应的变量副本,以防止内存泄漏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值