欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Netty源碼解析NioEventLoop創(chuàng)建的構(gòu)造方法

 更新時(shí)間:2022年03月25日 15:37:22   作者:向南是個(gè)萬(wàn)人迷  
這篇文章主要介紹了Netty源碼解析NioEventLoopGroup之NioEventLoop創(chuàng)建的構(gòu)造方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前文傳送門:Netty源碼分析 NioEventLoop

NioEventLoopGroup之NioEventLoop的創(chuàng)建

回到上一小節(jié)的MultithreadEventExecutorGroup類的構(gòu)造方法:

protected MultithreadEventExecutorGroup(int nThreads, Executor executor, 
                                        EventExecutorChooserFactory chooserFactory, Object... args) {
    //代碼省略
    if (executor == null) {
        //創(chuàng)建一個(gè)新的線程執(zhí)行器(1)
        executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
    }
    //構(gòu)造NioEventLoop(2)
    children = new EventExecutor[nThreads];
    for (int i = 0; i < nThreads; i ++) {
        boolean success = false;
        try {
            children[i] = newChild(executor, args);
            success = true;
        } catch (Exception e) {
            throw new IllegalStateException("failed to create a child event loop", e);
        } finally {
           //代碼省略
        }
    }
    //創(chuàng)建線程選擇器(3)
    chooser = chooserFactory.newChooser(children);
    //代碼省略
}

我們來看第二步構(gòu)造NioEventLoop

這里通過 children = new EventExecutor[nThreads] 初始化了children屬性, 看下這個(gè)屬性的定義:

private final EventExecutor[] children

這里的children是EventExecutor類型的數(shù)組, 其實(shí)就是NioEventLoop的集合, 因?yàn)镹ioEventLoop也是EventExecutor的子類

所以這里初始化了children數(shù)組, 大小為參數(shù)nThreads傳入的線程數(shù)量, 默認(rèn)為cpu核數(shù)的兩倍

后面就是通過for循環(huán)來創(chuàng)建NioEventLoop線程,

在循環(huán)體里通過 children[i] = newChild(executor, args) 創(chuàng)建NioEventLoop, 我們跟newChild(executor, args)方法

因?yàn)槭荖ioEventLoopGroup調(diào)用的,所以跟到NioEventLoop的newChild方法中:

protected EventLoop newChild(Executor executor, Object... args) throws Exception {
    return new NioEventLoop(this, executor, (SelectorProvider) args[0], 
        ((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
}

這里我們看到創(chuàng)建了一個(gè)NioEventLoop對(duì)象, 其中this是NioEventLoopGroup自身, executor就是上一小節(jié)講到的線程執(zhí)行器

我們繼續(xù)跟到NioEventLoop的構(gòu)造方法

NioEventLoop(NioEventLoopGroup parent, Executor executor, SelectorProvider selectorProvider, 
             SelectStrategy strategy, RejectedExecutionHandler rejectedExecutionHandler) {
    super(parent, executor, false, DEFAULT_MAX_PENDING_TASKS, rejectedExecutionHandler);
    //代碼省略
    provider = selectorProvider;
    selector = openSelector();
    selectStrategy = strategy;
}

首先看到了調(diào)用了父類的構(gòu)造方法, 然后初始化了幾個(gè)屬性:

 selector = openSelector() 這種方式創(chuàng)建個(gè)NioEventLoop綁定的selector對(duì)象, 有關(guān)創(chuàng)建過程, 之后會(huì)講到

跟進(jìn)父類SingleThreadEventLoop類構(gòu)造方法:

protected SingleThreadEventLoop(EventLoopGroup parent, Executor executor, 
                                boolean addTaskWakesUp, int maxPendingTasks, 
                                RejectedExecutionHandler rejectedExecutionHandler) {
    super(parent, executor, addTaskWakesUp, maxPendingTasks, rejectedExecutionHandler);
    tailTasks = newTaskQueue(maxPendingTasks);
}

再跟到父類SingleThreadEventExecutor構(gòu)造方法:

protected SingleThreadEventExecutor(EventExecutorGroup parent, Executor executor, 
                                    boolean addTaskWakesUp, int maxPendingTasks, 
                                    RejectedExecutionHandler rejectedHandler) {
    super(parent);
    this.addTaskWakesUp = addTaskWakesUp;
    this.maxPendingTasks = Math.max(16, maxPendingTasks);
    this.executor = ObjectUtil.checkNotNull(executor, "executor");
    taskQueue = newTaskQueue(this.maxPendingTasks);
    rejectedExecutionHandler = ObjectUtil.checkNotNull(rejectedHandler, "rejectedHandler");
}

 this.executor = ObjectUtil.checkNotNull(executor, "executor") 

這里初始化了線程執(zhí)行器

 taskQueue = newTaskQueue(this.maxPendingTasks) 

是創(chuàng)建一個(gè)任務(wù)隊(duì)列, 這個(gè)任務(wù)隊(duì)列可以將不屬于NioEventLoop線程的任務(wù)放到這個(gè)任務(wù)隊(duì)列中, 通過NioEventLoop線程執(zhí)行, 具體使用場(chǎng)景之后我們會(huì)看到

跟到父類AbstractScheduledEventExecutor的構(gòu)造方法中:

protected AbstractScheduledEventExecutor(EventExecutorGroup parent) {
    super(parent);
}

最后跟到AbstractEventExecutor類的構(gòu)造方法

protected AbstractEventExecutor(EventExecutorGroup parent) {
    this.parent = parent;
}

這里初始化了parent, 這個(gè)parent就NioEventLoop所屬的線程組NioEventLoopGroup對(duì)象

至此, NioEventLoop創(chuàng)建完成

以上就是Netty源碼解析NioEventLoop創(chuàng)建的構(gòu)造方法的詳細(xì)內(nèi)容,更多關(guān)于Netty NioEventLoop構(gòu)造方法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java中方法的使用、重載與遞歸的詳細(xì)介紹

    Java中方法的使用、重載與遞歸的詳細(xì)介紹

    前面我們提到了方法需要參數(shù)類型,但是如果我們需要用一個(gè)函數(shù)同時(shí)兼容多種參數(shù)的情況應(yīng)該怎么辦呢? 這里就可以使用到方法重載,對(duì)Java中方法的使用、重載與遞歸相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-11-11
  • eclipse自動(dòng)提示和自動(dòng)補(bǔ)全功能實(shí)現(xiàn)方法

    eclipse自動(dòng)提示和自動(dòng)補(bǔ)全功能實(shí)現(xiàn)方法

    這篇文章主要介紹了eclipse自動(dòng)提示和自動(dòng)補(bǔ)全的相關(guān)內(nèi)容,文中向大家分享了二者的實(shí)現(xiàn)方法代碼,需要的朋友可以了解下。
    2017-09-09
  • JAVA堆排序算法的講解

    JAVA堆排序算法的講解

    這篇文章主要介紹了JAVA堆排序算法的知識(shí)點(diǎn),文中代碼非常詳細(xì),配合上圖片講解,幫助大家更好的參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • JMeter中的后端監(jiān)聽器的實(shí)現(xiàn)

    JMeter中的后端監(jiān)聽器的實(shí)現(xiàn)

    本文主要介紹了JMeter中的后端監(jiān)聽器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • java使用Cookie判斷用戶登錄情況的方法

    java使用Cookie判斷用戶登錄情況的方法

    這篇文章主要為大家詳細(xì)介紹了java使用Cookie判斷用戶登錄情況,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Java switch多值匹配操作詳解

    Java switch多值匹配操作詳解

    這篇文章主要介紹了Java switch多值匹配操作詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Java Collection 移除元素方法及注意事項(xiàng)

    Java Collection 移除元素方法及注意事項(xiàng)

    這篇文章主要介紹了Java Collection 移除元素方法及注意事項(xiàng),通過一個(gè)簡(jiǎn)單實(shí)例給大家講解,需要的朋友可以參考下
    2020-01-01
  • Spring IOC基于注解啟動(dòng)示例詳析

    Spring IOC基于注解啟動(dòng)示例詳析

    這篇文章主要給大家介紹了Spring IOC基于注解啟動(dòng)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • JAVA進(jìn)階之HashMap底層實(shí)現(xiàn)解析

    JAVA進(jìn)階之HashMap底層實(shí)現(xiàn)解析

    Hashmap是java面試中經(jīng)常遇到的面試題,大部分都會(huì)問其底層原理與實(shí)現(xiàn),為了能夠溫故而知新,特地寫了這篇文章,以便時(shí)時(shí)學(xué)習(xí)
    2021-11-11
  • Tomcat 實(shí)現(xiàn)WebSocket詳細(xì)介紹

    Tomcat 實(shí)現(xiàn)WebSocket詳細(xì)介紹

    這篇文章主要介紹了Tomcat 如何實(shí)現(xiàn)WebSocket的相關(guān)資料,對(duì)WebSocket協(xié)議通信的過程進(jìn)行了詳細(xì)介紹,需要的朋友可以參考下
    2016-12-12

最新評(píng)論