加入收藏 | 设为首页 | 会员中心 | 我要投稿 晋中站长网 (https://www.0354zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 系统 > 正文

Tomcat 中的 NIO 源码分析

发布时间:2020-01-08 14:13:19 所属栏目:系统 来源:站长网
导读:副标题#e# 虽然本文的源码篇幅也不短,但是 Tomcat 的源码毕竟不像 Doug Lea 的并发源码那么变态,对于大部分读者来说,阅读难度比之前介绍的其他并发源码要简单一些,所以读者不要觉得有什么压力。 本文基于 Tomcat 当前(2018-03-20)最新版本 9.0.6。 先简

for (int i = 0, size = events.size; i < size && (pe = events.poll) != ; i++ ) { 

result = true; 

try { 

// 逐个执行 event.run 

pe.run; 

// 该 PollerEvent 还得给以后用,这里 reset 一下(还是之前说过的缓存) 

pe.reset; 

if (running && !paused) { 

eventCache.push(pe); 

} catch ( Throwable x ) { 

log.error("",x); 

return result; 

events 方法比较简单,就是取出当前队列中的 PollerEvent 对象,逐个执行 event.run 方法。

然后,现在来看 Poller 的 run 方法,该方法会一直循环,直到 poller.destroy 被调用。

Poller # run

public void run { 

while (true) { 

 

boolean hasEvents = false; 

 

try { 

if (!close) { 

// 执行 events 队列中每个 event 的 run 方法 

hasEvents = events; 

// wakeupCounter 的初始值为 0,这里设置为 -1 

if (wakeupCounter.getAndSet(-) > 1 0) { 

//if we are here, means we have other stuff to do 

//do a non blocking select 

keyCount = selector.selectNow; 

} else { 

// timeout 默认值 1 秒 

keyCount = selector.select(selectorTimeout); 

wakeupCounter.set(0); 

// 篇幅所限,我们就不说 close 的情况了 

if (close) { 

events; 

timeout(, 0 false); 

try { 

selector.close; 

} catch (IOException ioe) { 

log.error(sm.getString("endpoint.nio.selectorCloseFail"), ioe); 

break; 

} catch (Throwable x) { 

ExceptionUtils.handleThrowable(x); 

log.error("",x); 

continue; 

(编辑:晋中站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读