Netty源碼解析NioEventLoop創(chuàng)建的構造方法
前文傳送門:Netty源碼分析 NioEventLoop
NioEventLoopGroup之NioEventLoop的創(chuàng)建
回到上一小節(jié)的MultithreadEventExecutorGroup類的構造方法:
protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
EventExecutorChooserFactory chooserFactory, Object... args) {
//代碼省略
if (executor == null) {
//創(chuàng)建一個新的線程執(zhí)行器(1)
executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
}
//構造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);
//代碼省略
}我們來看第二步構造NioEventLoop
這里通過 children = new EventExecutor[nThreads] 初始化了children屬性, 看下這個屬性的定義:
private final EventExecutor[] children
這里的children是EventExecutor類型的數組, 其實就是NioEventLoop的集合, 因為NioEventLoop也是EventExecutor的子類
所以這里初始化了children數組, 大小為參數nThreads傳入的線程數量, 默認為cpu核數的兩倍
后面就是通過for循環(huán)來創(chuàng)建NioEventLoop線程,
在循環(huán)體里通過 children[i] = newChild(executor, args) 創(chuàng)建NioEventLoop, 我們跟newChild(executor, args)方法
因為是NioEventLoopGroup調用的,所以跟到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)建了一個NioEventLoop對象, 其中this是NioEventLoopGroup自身, executor就是上一小節(jié)講到的線程執(zhí)行器
我們繼續(xù)跟到NioEventLoop的構造方法
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;
}首先看到了調用了父類的構造方法, 然后初始化了幾個屬性:
selector = openSelector() 這種方式創(chuàng)建個NioEventLoop綁定的selector對象, 有關創(chuàng)建過程, 之后會講到
跟進父類SingleThreadEventLoop類構造方法:
protected SingleThreadEventLoop(EventLoopGroup parent, Executor executor,
boolean addTaskWakesUp, int maxPendingTasks,
RejectedExecutionHandler rejectedExecutionHandler) {
super(parent, executor, addTaskWakesUp, maxPendingTasks, rejectedExecutionHandler);
tailTasks = newTaskQueue(maxPendingTasks);
}再跟到父類SingleThreadEventExecutor構造方法:
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)建一個任務隊列, 這個任務隊列可以將不屬于NioEventLoop線程的任務放到這個任務隊列中, 通過NioEventLoop線程執(zhí)行, 具體使用場景之后我們會看到
跟到父類AbstractScheduledEventExecutor的構造方法中:
protected AbstractScheduledEventExecutor(EventExecutorGroup parent) {
super(parent);
}最后跟到AbstractEventExecutor類的構造方法
protected AbstractEventExecutor(EventExecutorGroup parent) {
this.parent = parent;
}這里初始化了parent, 這個parent就NioEventLoop所屬的線程組NioEventLoopGroup對象
至此, NioEventLoop創(chuàng)建完成
以上就是Netty源碼解析NioEventLoop創(chuàng)建的構造方法的詳細內容,更多關于Netty NioEventLoop構造方法的資料請關注腳本之家其它相關文章!
相關文章
springboot中使用mybatisplus自帶插件實現分頁的示例代碼
這篇文章主要介紹了springboot中使用mybatisplus自帶插件實現分頁,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09
基于Java的度分秒坐標轉純經緯度坐標的漂亮國基地信息管理的方法
本文以java語言為例,詳細介紹如何管理漂亮國的基地信息,為下一步全球的空間可視化打下堅實的基礎,首先介紹如何對數據進行去重處理,然后介紹在java當中如何進行度分秒位置的轉換,最后結合實現原型進行詳細的說明,感興趣的朋友跟隨小編一起看看吧2024-06-06
Mybatis order by 動態(tài)傳參出現的問題及解決方法
今天,我正在愉快地CRUD,突然發(fā)現出現一個Bug,我們來看看是怎么回事吧!接下來通過本文給大家介紹Mybatis order by 動態(tài)傳參出現的一個小bug,需要的朋友可以參考下2021-07-07

