Netty源碼分析NioEventLoop初始化線程選擇器創(chuàng)建
前文傳送門:NioEventLoop創(chuàng)建
初始化線程選擇器
回到上一小節(jié)的MultithreadEventExecutorGroup類的構(gòu)造方法:
protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
EventExecutorChooserFactory chooserFactory, Object... args) {
//代碼省略
if (executor == null) {
//創(chuàng)建一個新的線程執(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);
//代碼省略
}我們看第三步, 創(chuàng)建線程選擇器:
chooser = chooserFactory.newChooser(children);
NioEventLoop都綁定一個chooser對象, 作為線程選擇器, 通過這個線程選擇器, 為每一個channel分配不同的線程
我們看到newChooser(children)傳入了NioEventLoop數(shù)組
我們跟到DefaultEventExecutorChooserFactory類中的newChooser方法:
public EventExecutorChooser newChooser(EventExecutor[] executors) {
if (isPowerOfTwo(executors.length)) {
return new PowerOfTowEventExecutorChooser(executors);
} else {
return new GenericEventExecutorChooser(executors);
}
}這里通過 isPowerOfTwo(executors.length) 判斷NioEventLoop的線程數(shù)是不是2的倍數(shù), 然后根據(jù)判斷結(jié)果返回兩種選擇器對象, 這里使用到j(luò)ava設(shè)計模式的策略模式
根據(jù)這兩個類的名字不難看出, 如果是2的倍數(shù), 使用的是一種高性能的方式選擇線程, 如果不是2的倍數(shù), 則使用一種比較普通的線程選擇方式
我們簡單跟進這兩種策略的選擇器對象中看一下, 首先看一下PowerOfTowEventExecutorChooser這個類:
private static final class PowerOfTowEventExecutorChooser implements EventExecutorChooser {
private final AtomicInteger idx = new AtomicInteger();
private final EventExecutor[] executors;
PowerOfTowEventExecutorChooser(EventExecutor[] executors) {
this.executors = executors;
}
@Override
public EventExecutor next() {
return executors[idx.getAndIncrement() & executors.length - 1];
}
}這個類實現(xiàn)了線程選擇器的接口EventExecutorChooser, 構(gòu)造方法中初始化了NioEventLoop線程數(shù)組
重點關(guān)注下next()方法, next()方法就是選擇下一個線程的方法, 如果線程數(shù)是2的倍數(shù), 這里通過按位與進行計算, 所以效率極高
再看一下GenericEventExecutorChooser這個類:
private static final class GenericEventExecutorChooser implements EventExecutorChooser {
private final AtomicInteger idx = new AtomicInteger();
private final EventExecutor[] executors;
GenericEventExecutorChooser(EventExecutor[] executors) {
this.executors = executors;
}
@Override
public EventExecutor next() {
return executors[Math.abs(idx.getAndIncrement() % executors.length)];
}
}這個類同樣實現(xiàn)了線程選擇器的接口EventExecutorChooser, 并在造方法中初始化了NioEventLoop線程數(shù)組
再看這個類的next()方法, 如果線程數(shù)不是2的倍數(shù), 則用絕對值和取模的這種效率一般的方式進行線程選擇
這樣, 我們就初始化了線程選擇器對象
以上就是Netty源碼分析NioEventLoop初始化線程選擇器創(chuàng)建的詳細內(nèi)容,更多關(guān)于Netty線程選擇器NioEventLoop的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring框架開發(fā)scope作用域分析總結(jié)
這篇文章主要介紹了Spring框架開發(fā)中scope作用域的分析總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2021-09-09
Java使用POI-TL和JFreeChart動態(tài)生成Word報告
本文介紹了使用POI-TL和JFreeChart生成包含動態(tài)數(shù)據(jù)和圖表的Word報告的方法,并分享了實際開發(fā)中的踩坑經(jīng)驗,通過代碼示例講解的非常詳細,具有一定的參考價值,需要的朋友可以參考下2025-02-02
NetBeans安裝提示neatbeans cannot find java 1.8 or higher
今天小編就為大家分享一篇關(guān)于NetBeans安裝提示neatbeans cannot find java 1.8 or higher,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04
Spring?cloud?Hystrix注解初始化源碼過程解讀
這篇文章主要為大家介紹了Hystrix初始化部分,我們從源碼的角度分析一下@EnableCircuitBreaker以及@HystrixCommand注解的初始化過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-12-12
Android中比較常見的Java super關(guān)鍵字
這篇文章主要為大家介紹了Android中比較常見的Java super關(guān)鍵字,具有一定的學習參考價值,感興趣的小伙伴們可以參考一下2016-01-01
一篇文章帶了解如何用SpringBoot在RequestBody中優(yōu)雅的使用枚舉參數(shù)
這篇文章主要介紹了SpringBoot中RequestBodyAdvice使用枚舉參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringCloud中Gateway實現(xiàn)鑒權(quán)的方法
本文主要介紹了SpringCloud中Gateway實現(xiàn)鑒權(quán)的方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11

