亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java.security.egd?作用詳解

 更新時間:2023年08月22日 09:34:14   作者:明洋的生活分享  
這篇文章主要為大家介紹了java.security.egd作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

SecureRandom

在java各種組件中使用廣泛,可以可靠的產(chǎn)生隨機數(shù)。但在大量產(chǎn)生隨機數(shù)的場景下,性能會較低。這時可以使用"-Djava.security.egd=file:/dev/./urandom"加快隨機數(shù)產(chǎn)生過程。

以產(chǎn)生uuid的時候使用nextBytes產(chǎn)生隨機數(shù)為入口,我們看一下SecureRandom的代碼邏輯。

public static UUID randomUUID() {
        SecureRandom ng =Holder.numberGenerator;
        byte[] randomBytes = new byte[16];
        ng.nextBytes(randomBytes);
        randomBytes[6] &= 0x0f;  /* clear version       */
        randomBytes[6]  |=0x40;  /* set to version 4     */
        randomBytes[8] &= 0x3f;  /* clear variant       */
        randomBytes[8]  |=0x80;  /* set to IETF variant  */
        return newUUID(randomBytes);
    }

使用SecureRandom.next*的方法

在使用SecureRandom產(chǎn)生下一個隨機數(shù)的時候調(diào)用nextLong或者nextBytes,最終會調(diào)用SecureRandom的nextBytes。

public long nextLong() { 
        // it's okay that the bottom wordremains signed. 
        return ((long)(next(32)) << 32)+ next(32); 
    } 
    final protected int next(int numBits) { 
        int numBytes = (numBits+7)/8; 
        byte b[] = new byte[numBytes]; 
        int next = 0; 
        nextBytes(b);
        for (int i = 0; i < numBytes; i++) 
            next = (next << 8)+ (b[i] & 0xFF); 
        return next >>> (numBytes*8 -numBits); 
    }

而nextBytes是一個同步的方法,在多線程使用時,可能會產(chǎn)生性能瓶頸。

synchronized public void nextBytes(byte[] bytes) { 
       secureRandomSpi.engineNextBytes(bytes); 
    }

secureRandomSpi被初始化為sun.security.provider.SecureRandom

secureRandomSpi是SecureRandom.NativePRNG的一個實例。

使用jvm參數(shù)-Djava.security.debug=all

可以打印securityprovider列表,從中可以看出,SecureRandom.NativePRNG由sun.security.provider.NativePRNG提供服務(wù)。

Provider: Set SUN provider property[SecureRandom.NativePRNG/sun.security.provider.NativePRNG]

分析openjdk的源碼,NativePRNG.engineNextBytes調(diào)用了NativePRNG.RandomIO.ensureBufferValid,而ensureBufferValid直接從urandom讀取數(shù)據(jù):

private void ensureBufferValid() throws IOException {
            ...
            readFully(urandomIn, urandomBuffer);
            ...
        }

通過測試可以發(fā)現(xiàn)**,hotspot需要使用配置項"-Djava.security.egd=file:/dev/./urandom"才能從urandom讀取數(shù)據(jù),這里openjdk做了優(yōu)化,直接從urandom讀取數(shù)據(jù)**。

/dev/random在產(chǎn)生大量隨機數(shù)的時候比/dev/urandom慢,所以,建議在大量使用隨機數(shù)的時候,將隨機數(shù)發(fā)生器指定為/dev/./urandom。

注意:jvm參數(shù)值為/dev/./urandom而不是/dev/urandom,這里是jdk的一個bug引起。

bug產(chǎn)生的原因

bug產(chǎn)生的原因請注意下面第四行源碼,如果java.security.egd參數(shù)指定的是file:/dev/random或者file:/dev/urandom,則調(diào)用了無參的NativeSeedGenerator構(gòu)造函數(shù),而無參的構(gòu)造函數(shù)將默認使用file:/dev/random 。

openjdk的代碼和hotspot的代碼已經(jīng)不同,openjdk在后續(xù)產(chǎn)生隨機數(shù)的時候沒有使用這個變量。

abstract class SeedGenerator {
......
    static {
        String egdSource = SunEntries.getSeedSource();
        if (egdSource.equals(URL_DEV_RANDOM) || egdSource.equals(URL_DEV_URANDOM)) {
            try {
                instance = new NativeSeedGenerator();
                if (debug != null) {
                    debug.println("Using operating system seed generator");
                }
            } catch (IOException e) {
                if (debug != null) {
                    debug.println("Failed to use operating system seed "
                                  + "generator: " + e.toString());
                }
            }
        } else if (egdSource.length() != 0) {
            try {
                instance = new URLSeedGenerator(egdSource);
                if (debug != null) {
                    debug.println("Using URL seed generator reading from "
                                  + egdSource);
                }
            } catch (IOException e) {
                if (debug != null)
                    debug.println("Failed to create seed generator with "
                                  + egdSource + ": " + e.toString());
            }
        }
......
    }

在啟動應(yīng)用時配置 -Djava.security.egd=file:/dev/./urandom 可以一定程度上加快應(yīng)用啟動。

以上就是java.security.egd 作用詳解的詳細內(nèi)容,更多關(guān)于java.security.egd 作用的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 淺談Spring boot cache使用和原理

    淺談Spring boot cache使用和原理

    這篇文章主要介紹了淺談Spring boot cache使用和原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • 使用springboot aop來實現(xiàn)讀寫分離和事物配置

    使用springboot aop來實現(xiàn)讀寫分離和事物配置

    這篇文章主要介紹了使用springboot aop來實現(xiàn)讀寫分離和事物配置,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • SpringBoot解決@Component無法注入其他Bean的問題

    SpringBoot解決@Component無法注入其他Bean的問題

    這篇文章主要介紹了SpringBoot解決@Component無法注入其他Bean的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java本地緩存的實現(xiàn)代碼

    Java本地緩存的實現(xiàn)代碼

    本篇文章主要介紹了Java本地緩存的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • java8 List<Object>去掉重復(fù)對象的幾種方法

    java8 List<Object>去掉重復(fù)對象的幾種方法

    本文主要介紹了java8 List<Object>去掉重復(fù)對象的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Java中如何編寫一個數(shù)的n次方(冪運算)?

    Java中如何編寫一個數(shù)的n次方(冪運算)?

    本文介紹了使用pow函數(shù)和自定義for循環(huán)計算冪的O(n)時間復(fù)雜度方法,然后重點講解了快速冪算法的分治思想,以及從二進制角度的解釋,包括如何通過位運算和循環(huán)迭代實現(xiàn)高效計算,給出了Java代碼實現(xiàn)
    2024-07-07
  • Spring Boot實現(xiàn)簡單的定時任務(wù)

    Spring Boot實現(xiàn)簡單的定時任務(wù)

    這篇文章主要給大家介紹了關(guān)于利用Spring Boot實現(xiàn)簡單的定時任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 詳解Spring依賴注入的三種方式以及優(yōu)缺點

    詳解Spring依賴注入的三種方式以及優(yōu)缺點

    IoC?和?DI?是?Spring?中最重要的兩個概念,其中?IoC(Inversion?of?Control)為控制反轉(zhuǎn)的思想,而?DI(Dependency?Injection)依賴注入為其(IoC)具體實現(xiàn)。那么?DI?實現(xiàn)依賴注入的方式有幾種?這些注入方式又有什么不同?本文就來和大家一起詳細聊聊
    2022-08-08
  • java中continue和break區(qū)別詳細解析

    java中continue和break區(qū)別詳細解析

    break和continue都是跳轉(zhuǎn)語句,它們將程序的控制權(quán)轉(zhuǎn)移到程序的另一部分,下面這篇文章主要給大家介紹了關(guān)于java中continue和break區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • SpringBoot實現(xiàn)日志鏈路追蹤的項目實踐

    SpringBoot實現(xiàn)日志鏈路追蹤的項目實踐

    在分布式系統(tǒng)中,由于請求的處理過程可能會跨越多個服務(wù),因此,對請求的追蹤變得尤為重要,本文主要介紹了SpringBoot實現(xiàn)日志鏈路追蹤的項目實踐,感興趣的可以了解一下
    2024-03-03

最新評論