NioEventLoop 的实例化过程
生活随笔
收集整理的這篇文章主要介紹了
NioEventLoop 的实例化过程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
先簡單回顧一下EventLoop 實例化的運行時序圖:
從上圖可以看到, SingleThreadEventExecutor 有一個名為thread 的Thread 類型字段, 這個字段就是與SingleThreadEventExecutor 關(guān)聯(lián)的本地線程。我們看看thread 在哪里被賦值的:?
private void doStartThread() {assert thread == null;executor.execute(new Runnable() {@Overridepublic void run() {thread = Thread.currentThread();boolean success = false;updateLastExecutionTime();try {SingleThreadEventExecutor.this.run();success = true;} catch (Throwable t) {logger.warn("Unexpected exception from an event executor: ", t);} finally {// 此處省略清理代碼}}}); }之前的章節(jié)我們分析過,SingleThreadEventExecutor 啟動時會調(diào)用doStartThread()方法,然后調(diào)用executor.execute()方法,將當(dāng)前線程賦值給thread。在這個線程中所做的事情主要就是調(diào)用SingleThreadEventExecutor.this.run()方法,而因為NioEventLoop 實現(xiàn)了這個方法,因此根據(jù)多態(tài)性,其實調(diào)用的是NioEventLoop.run()方法。
?
總結(jié)
以上是生活随笔為你收集整理的NioEventLoop 的实例化过程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 任务执行者EventLoop
- 下一篇: EventLoop 与Channel 的