Netty源碼解析NioEventLoop創(chuàng)建的構(gòu)造方法
前文傳送門: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)文章!
- Netty分布式NioSocketChannel注冊(cè)到selector方法解析
- Netty客戶端接入流程N(yùn)ioSocketChannel創(chuàng)建解析
- Netty分布式NioEventLoop任務(wù)隊(duì)列執(zhí)行源碼分析
- Netty源碼分析NioEventLoop執(zhí)行select操作入口
- Netty分布式NioEventLoop優(yōu)化selector源碼解析
- Netty源碼分析NioEventLoop線程的啟動(dòng)
- Netty源碼分析NioEventLoop初始化線程選擇器創(chuàng)建
- Netty實(shí)戰(zhàn)源碼解析NIO編程
相關(guā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-09JMeter中的后端監(jiān)聽器的實(shí)現(xiàn)
本文主要介紹了JMeter中的后端監(jiān)聽器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Java Collection 移除元素方法及注意事項(xiàng)
這篇文章主要介紹了Java Collection 移除元素方法及注意事項(xiàng),通過一個(gè)簡(jiǎn)單實(shí)例給大家講解,需要的朋友可以參考下2020-01-01JAVA進(jìn)階之HashMap底層實(shí)現(xiàn)解析
Hashmap是java面試中經(jīng)常遇到的面試題,大部分都會(huì)問其底層原理與實(shí)現(xiàn),為了能夠溫故而知新,特地寫了這篇文章,以便時(shí)時(shí)學(xué)習(xí)2021-11-11Tomcat 實(shí)現(xiàn)WebSocket詳細(xì)介紹
這篇文章主要介紹了Tomcat 如何實(shí)現(xiàn)WebSocket的相關(guān)資料,對(duì)WebSocket協(xié)議通信的過程進(jìn)行了詳細(xì)介紹,需要的朋友可以參考下2016-12-12