栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ThreadLocalRandom类用法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ThreadLocalRandom类用法

JDK7的JUC包新引入了 ThreadLocalRandom 这个类,来应对多线程场景下随机数生成问题。

以前生成随机数一般用Random类,虽然该类是线程安全的,但是底层用的CAS,在多线程环境下效率会降低,故此JDK7以后推荐用 ThreadLocalRandom来生成随机数。(Random和ThreadLocalRandom生成的都是伪随机数,可被预测,要是对随机数安全性有要求,应考虑Random的另一个子类 SecureRandom)

简单看一下ThreadLocal的源码,发现它是单例的

    
    private ThreadLocalRandom() {
        initialized = true; // false during super() call
    }

    
    static final ThreadLocalRandom instance = new ThreadLocalRandom();
再看类的文档描述
* 

Usages of this class should typically be of the form: * {@code ThreadLocalRandom.current().nextX(...)} (where * {@code X} is {@code Int}, {@code Long}, etc). * When all usages are of this form, it is never possible to * accidently share a {@code ThreadLocalRandom} across multiple threads.

要获取它的单例对象,应使用current()方法,其他的操作就和Random一样,比如nextInt()、nextLong()、nextDouble()等等。

用法示例:

public class ThreadLocalRandomTest {

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> System.out.println(
                    Thread.currentThread().getName() + "生成的随机数: " +
                    ThreadLocalRandom.current().nextInt(10))).start();
        }
    }
}

 执行的结果大致如下:

最后经过查阅不同大佬的文章,发现ThreadLocalRandom使用的时候有个坑,要注意,在多线程时,要保证每个线程单独持有它的实例,而不是线程公有,比如将上面的代码调整一下,如下

错误示例:

public class ThreadLocalRandomTest {

    public static void main(String[] args) {
        // 错误用法,会导致多线程产生相同的随机数
        ThreadLocalRandom random = ThreadLocalRandom.current();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> System.out.println(
                    Thread.currentThread().getName() + "生成的随机数: " +
                    random.nextInt(10))).start();
        }
    }
}

 执行的结果大致如下:

发现竟然生成了相同的随机数!!!

产生该错误的原因,可以参考该回答

ThreadLocalRandom为什么多线程随机数相同? - 知乎谢邀~因为ThreadLocalRandom用法并不是这样的,请读一下current()方法的源码注释 /** * Returns …https://www.zhihu.com/question/376227700/answer/1052322532

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678815.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号