当前位置:
首页 >
java中ThreadLocalRandom的使用
发布时间:2024/2/28
44
豆豆
生活随笔
收集整理的这篇文章主要介绍了
java中ThreadLocalRandom的使用
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
java中ThreadLocalRandom的使用
在java中我们通常会需要使用到java.util.Random来便利的生产随机数。但是Random是线程安全的,如果要在线程环境中的话就有可能产生性能瓶颈。
我们以Random中常用的nextInt方法为例来具体看一下:
public int nextInt() {return next(32);}nextInt方法实际上调用了下面的方法:
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));}从代码中我们可以看到,方法内部使用了AtomicLong,并调用了它的compareAndSet方法来保证线程安全性。所以这个是一个线程安全的方法。
其实在多个线程环境中,Random根本就需要共享实例,那么该怎么处理呢?
在JDK 7 中引入了一个ThreadLocalRandom的类。ThreadLocal大家都知道就是线程的本地变量,而ThreadLocalRandom就是线程本地的Random。
我们看下怎么调用:
ThreadLocalRandom.current().nextInt();我们来为这两个类分别写一个benchMark测试:
public class RandomUsage {public void testRandom() throws InterruptedException {ExecutorService executorService=Executors.newFixedThreadPool(2);Random random = new Random();List<Callable<Integer>> callables = new ArrayList<>();for (int i = 0; i < 1000; i++) {callables.add(() -> {return random.nextInt();});}executorService.invokeAll(callables);}public static void main(String[] args) throws RunnerException {Options opt = new OptionsBuilder().include(RandomUsage.class.getSimpleName())// 预热5轮.warmupIterations(5)// 度量10轮.measurementIterations(10).forks(1).build();new Runner(opt).run();} } public class ThreadLocalRandomUsage {@Benchmark@BenchmarkMode(Mode.AverageTime)@OutputTimeUnit(TimeUnit.MICROSECONDS)public void testThreadLocalRandom() throws InterruptedException {ExecutorService executorService=Executors.newFixedThreadPool(2);List<Callable<Integer>> callables = new ArrayList<>();for (int i = 0; i < 1000; i++) {callables.add(() -> {return ThreadLocalRandom.current().nextInt();});}executorService.invokeAll(callables);}public static void main(String[] args) throws RunnerException {Options opt = new OptionsBuilder().include(ThreadLocalRandomUsage.class.getSimpleName())// 预热5轮.warmupIterations(5)// 度量10轮.measurementIterations(10).forks(1).build();new Runner(opt).run();} }分析运行结果,我们可以看出ThreadLocalRandom在多线程环境中会比Random要快。
本文的例子可以参考https://github.com/ddean2009/learn-java-concurrency/tree/master/ThreadLocalRandom
更多精彩内容且看:
- 区块链从入门到放弃系列教程-涵盖密码学,超级账本,以太坊,Libra,比特币等持续更新
- Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新
- Spring 5.X系列教程:满足你对Spring5的一切想象-持续更新
- java程序员从小工到专家成神之路(2020版)-持续更新中,附详细文章教程
更新文章请参考flydean的博客
总结
以上是生活随笔为你收集整理的java中ThreadLocalRandom的使用的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 在java中使用JMH(Java Mic
- 下一篇: java中FutureTask的使用