定位
监听TCP连接的后台线程,并转发给适当的处理器。
tomcat源码注释:
/**
* The background thread that listens for incoming TCP/IP connections and
* hands them off to an appropriate processor.
*/
类结构
Acceptor作为内部类定义在AbstractEndpoint中(AbstractEndpoint是一个抽象类,提供了实现框架,不同的协议需要提供不同的Endpoint.这个类的作用就是提供底层的网络I/O的处理),实现了Runnable接口。Http1.1默认采用BIO,Endpoint实现类用的JIoEndpoint。
有3种不同的Acceptor
父类中定义了其状态和线程名称
public abstract static class Acceptor implements Runnable {
public enum AcceptorState {
NEW, RUNNING, PAUSED, ENDED
}
protected volatile AcceptorState state = AcceptorState.NEW;
public final AcceptorState getState() {
return state;
}
private String threadName;
protected final void setThreadName(final String threadName) {
this.threadName = threadName;
}
protected final String getThreadName() {
return threadName;
}
}
线程属性
线程名称
http-bio-[InetAddress]-[port]-Acceptor-Number 如:默认为http-bio-8080-Acceptor-0
优先级
线程默认优先级,5
是否Daemon
default 守护线程模式运行
线程个数
tomcat7 默认启动1个 Acceptor线程,多颗CPU的话可配置更大一点。
<attribute name="acceptorThreadCount" required="false">
<p>The number of threads to be used to accept connections. Increase this
value on a multi CPU machine, although you would never really need more
than <code>2</code>. Also, with a lot of non keep alive connections, you
might want to increase this value as well. Default value is
<code>1</code>.</p>
</attribute>
具体所做的事情
running状态
- 获取一个Connector连接,如果已达到最大连接数将等待
//if we have reached max connections, wait
countUpOrAwaitConnection();
2 调用serverSocket.accept方法监听连接请求
3 将socket上的请求转发给工作线程池,转发成功则该socket可以被复用,是keep-alive的;否则会关闭该socket,并将Connector可用连接数+1.
4 循环上述步骤1-3.
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/105370.html