Tomcat7 Acceptor线程学习

来源:互联网 发布:阿里云和华为云对比 编辑:程序博客网 时间:2024/06/05 17:31

定位

监听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状态

  1. 获取一个Connector连接,如果已达到最大连接数将等待
//if we have reached max connections, wait                    countUpOrAwaitConnection();

2 调用serverSocket.accept方法监听连接请求
3 将socket上的请求转发给工作线程池,转发成功则该socket可以被复用,是keep-alive的;否则会关闭该socket,并将Connector可用连接数+1.
4 循环上述步骤1-3.

0 0
原创粉丝点击