java中ThreadLocalRandom的使用詳解
在java中我們通常會(huì)需要使用到j(luò)ava.util.Random來(lái)便利的生產(chǎn)隨機(jī)數(shù)。但是Random是線程安全的,如果要在線程環(huán)境中的話就有可能產(chǎn)生性能瓶頸。
我們以Random中常用的nextInt方法為例來(lái)具體看一下:
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方法來(lái)保證線程安全性。所以這個(gè)是一個(gè)線程安全的方法。
其實(shí)在多個(gè)線程環(huán)境中,Random根本就需要共享實(shí)例,那么該怎么處理呢?
在JDK 7 中引入了一個(gè)ThreadLocalRandom的類。ThreadLocal大家都知道就是線程的本地變量,而ThreadLocalRandom就是線程本地的Random。
我們看下怎么調(diào)用:
ThreadLocalRandom.current().nextInt();
我們來(lái)為這兩個(gè)類分別寫(xiě)一個(gè)benchMark測(cè)試:
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
到此這篇關(guān)于java中ThreadLocalRandom的使用詳解的文章就介紹到這了,更多相關(guān)java ThreadLocalRandom內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JetBrains IntelliJ IDEA 2020安裝與使用教程詳解
這篇文章主要介紹了JetBrains IntelliJ IDEA 2020安裝與使用教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06MyBatis學(xué)習(xí)教程之開(kāi)發(fā)Dao的方法教程
這篇文章主要給大家介紹了關(guān)于MyBatis開(kāi)發(fā)Dao的相關(guān)資料,使用Mybatis開(kāi)發(fā)Dao,通常有兩個(gè)方法,即原始Dao開(kāi)發(fā)方法和Mapper接口開(kāi)發(fā)方法。文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面來(lái)一起看看吧。2017-07-07Java將字符串轉(zhuǎn)化為數(shù)組的兩種方法
Java中的String類是一種特殊的字符串,它可以被用于處理字符串,Java中的String類也可以將字符串轉(zhuǎn)換為數(shù)組,下面這篇文章主要給大家介紹了關(guān)于Java將字符串轉(zhuǎn)化為數(shù)組的兩種方法,需要的朋友可以參考下2023-05-05一篇文章帶你使用SpringBoot基于WebSocket的在線群聊實(shí)現(xiàn)
這篇文章主要介紹了一篇文章帶你使用SpringBoot基于WebSocket的在線群聊實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10java算法題解??虰M99順時(shí)針旋轉(zhuǎn)矩陣示例
這篇文章主要為大家介紹了java算法題解??虰M99順時(shí)針旋轉(zhuǎn)矩陣示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Spring ApplicationContext上下文核心容器深入探究
ApplicationContext是Spring應(yīng)用程序中的中央接口,由于繼承了多個(gè)組件,使得ApplicationContext擁有了許多Spring的核心功能,如獲取bean組件,注冊(cè)監(jiān)聽(tīng)事件,加載資源文件等2023-01-01