Tomcat7 Acceptor线程学习

定位

监听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.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值