日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java中ThreadLocalRandom的使用

發(fā)布時(shí)間:2024/2/28 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中ThreadLocalRandom的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

java中ThreadLocalRandom的使用

在java中我們通常會(huì)需要使用到j(luò)ava.util.Random來便利的生產(chǎn)隨機(jī)數(shù)。但是Random是線程安全的,如果要在線程環(huán)境中的話就有可能產(chǎn)生性能瓶頸。

我們以Random中常用的nextInt方法為例來具體看一下:

public int nextInt() {return next(32);}

nextInt方法實(shí)際上調(diào)用了下面的方法:

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

從代碼中我們可以看到,方法內(nèi)部使用了AtomicLong,并調(diào)用了它的compareAndSet方法來保證線程安全性。所以這個(gè)是一個(gè)線程安全的方法。

其實(shí)在多個(gè)線程環(huán)境中,Random根本就需要共享實(shí)例,那么該怎么處理呢?

在JDK 7 中引入了一個(gè)ThreadLocalRandom的類。ThreadLocal大家都知道就是線程的本地變量,而ThreadLocalRandom就是線程本地的Random。

我們看下怎么調(diào)用:

ThreadLocalRandom.current().nextInt();

我們來為這兩個(gè)類分別寫一個(gè)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())// 預(yù)熱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())// 預(yù)熱5輪.warmupIterations(5)// 度量10輪.measurementIterations(10).forks(1).build();new Runner(opt).run();} }

分析運(yùn)行結(jié)果,我們可以看出ThreadLocalRandom在多線程環(huán)境中會(huì)比Random要快。

本文的例子可以參考https://github.com/ddean2009/learn-java-concurrency/tree/master/ThreadLocalRandom

更多精彩內(nèi)容且看:

  • 區(qū)塊鏈從入門到放棄系列教程-涵蓋密碼學(xué),超級(jí)賬本,以太坊,Libra,比特幣等持續(xù)更新
  • Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續(xù)更新
  • Spring 5.X系列教程:滿足你對(duì)Spring5的一切想象-持續(xù)更新
  • java程序員從小工到專家成神之路(2020版)-持續(xù)更新中,附詳細(xì)文章教程

更新文章請(qǐng)參考flydean的博客

總結(jié)

以上是生活随笔為你收集整理的java中ThreadLocalRandom的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。