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)建一個(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); //代碼省略 }
我們看第三步, 創(chuàng)建線程選擇器:
chooser = chooserFactory.newChooser(children);
NioEventLoop都綁定一個(gè)chooser對(duì)象, 作為線程選擇器, 通過這個(gè)線程選擇器, 為每一個(gè)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é)果返回兩種選擇器對(duì)象, 這里使用到j(luò)ava設(shè)計(jì)模式的策略模式
根據(jù)這兩個(gè)類的名字不難看出, 如果是2的倍數(shù), 使用的是一種高性能的方式選擇線程, 如果不是2的倍數(shù), 則使用一種比較普通的線程選擇方式
我們簡單跟進(jìn)這兩種策略的選擇器對(duì)象中看一下, 首先看一下PowerOfTowEventExecutorChooser這個(gè)類:
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]; } }
這個(gè)類實(shí)現(xiàn)了線程選擇器的接口EventExecutorChooser, 構(gòu)造方法中初始化了NioEventLoop線程數(shù)組
重點(diǎn)關(guān)注下next()方法, next()方法就是選擇下一個(gè)線程的方法, 如果線程數(shù)是2的倍數(shù), 這里通過按位與進(jìn)行計(jì)算, 所以效率極高
再看一下GenericEventExecutorChooser這個(gè)類:
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)]; } }
這個(gè)類同樣實(shí)現(xiàn)了線程選擇器的接口EventExecutorChooser, 并在造方法中初始化了NioEventLoop線程數(shù)組
再看這個(gè)類的next()方法, 如果線程數(shù)不是2的倍數(shù), 則用絕對(duì)值和取模的這種效率一般的方式進(jìn)行線程選擇
這樣, 我們就初始化了線程選擇器對(duì)象
以上就是Netty源碼分析NioEventLoop初始化線程選擇器創(chuàng)建的詳細(xì)內(nèi)容,更多關(guān)于Netty線程選擇器NioEventLoop的資料請(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)建的構(gòu)造方法
- Netty實(shí)戰(zhàn)源碼解析NIO編程
相關(guān)文章
Java多線程之鎖的強(qiáng)化學(xué)習(xí)
Java多線程的鎖都是基于對(duì)象的,Java中的每一個(gè)對(duì)象都可以作為一個(gè)鎖。這篇文章主要來通過一下示例為大家強(qiáng)化一下鎖的相關(guān)知識(shí)的掌握,希望對(duì)大家有所幫助2023-02-02Java的@Transactional、@Aysnc、事務(wù)同步問題詳解
這篇文章主要介紹了Java的@Transactional、@Aysnc、事務(wù)同步問題詳解,現(xiàn)在我們需要在一個(gè)業(yè)務(wù)方法中插入一個(gè)用戶,這個(gè)業(yè)務(wù)方法我們需要加上事務(wù),然后插入用戶后,我們要異步的方式打印出數(shù)據(jù)庫中所有存在的用戶,需要的朋友可以參考下2023-11-11jpa異常No entity found for query問題解決
這篇文章主要為大家介紹了jpa異常之No entity found for query的異常問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03spring的TransactionalEventListener事務(wù)感知源碼解析
這篇文章主要為大家介紹了spring的TransactionalEventListener事務(wù)感知源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09詳解SpringBoot 發(fā)布ApplicationEventPublisher和監(jiān)聽ApplicationEvent事
這篇文章主要介紹了詳解SpringBoot 發(fā)布ApplicationEventPublisher和監(jiān)聽ApplicationEvent事件,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06java Socket實(shí)現(xiàn)簡單模擬HTTP服務(wù)器
這篇文章主要介紹了java Socket實(shí)現(xiàn)簡單模擬HTTP服務(wù)器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05