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對象, 其中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對象, 有關(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í)行, 具體使用場景之后我們會(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對象
至此, NioEventLoop創(chuàng)建完成
以上就是Netty源碼解析NioEventLoop創(chuàng)建的構(gòu)造方法的詳細(xì)內(nèi)容,更多關(guān)于Netty NioEventLoop構(gòu)造方法的資料請關(guān)注腳本之家其它相關(guān)文章!
- Netty分布式NioSocketChannel注冊到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)文章
淺談Spring裝配Bean之組件掃描和自動(dòng)裝配
本篇文章主要介紹了淺談Spring裝配Bean之組件掃描和自動(dòng)裝配,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
java -jar啟動(dòng)項(xiàng)目以及日志輸出的相關(guān)問題
這篇文章主要介紹了java -jar啟動(dòng)項(xiàng)目以及日志輸出的相關(guān)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
java多線程編程之向線程傳遞數(shù)據(jù)的三種方法
在多線程的異步開發(fā)模式下,數(shù)據(jù)的傳遞和返回和同步開發(fā)模式有很大的區(qū)別。由于線程的運(yùn)行和結(jié)束是不可預(yù)料的,因此,在傳遞和返回?cái)?shù)據(jù)時(shí)就無法象函數(shù)一樣通過函數(shù)參數(shù)和return語句來返回?cái)?shù)據(jù)2014-01-01
springboot中使用mybatisplus自帶插件實(shí)現(xiàn)分頁的示例代碼
這篇文章主要介紹了springboot中使用mybatisplus自帶插件實(shí)現(xiàn)分頁,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
基于Java的度分秒坐標(biāo)轉(zhuǎn)純經(jīng)緯度坐標(biāo)的漂亮國基地信息管理的方法
本文以java語言為例,詳細(xì)介紹如何管理漂亮國的基地信息,為下一步全球的空間可視化打下堅(jiān)實(shí)的基礎(chǔ),首先介紹如何對數(shù)據(jù)進(jìn)行去重處理,然后介紹在java當(dāng)中如何進(jìn)行度分秒位置的轉(zhuǎn)換,最后結(jié)合實(shí)現(xiàn)原型進(jìn)行詳細(xì)的說明,感興趣的朋友跟隨小編一起看看吧2024-06-06
解決resultMap映射數(shù)據(jù)錯(cuò)誤的問題
這篇文章主要介紹了解決resultMap映射數(shù)據(jù)錯(cuò)誤的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Mybatis order by 動(dòng)態(tài)傳參出現(xiàn)的問題及解決方法
今天,我正在愉快地CRUD,突然發(fā)現(xiàn)出現(xiàn)一個(gè)Bug,我們來看看是怎么回事吧!接下來通過本文給大家介紹Mybatis order by 動(dòng)態(tài)傳參出現(xiàn)的一個(gè)小bug,需要的朋友可以參考下2021-07-07

