【六祎 - Tomcat】Tomcat性能调优 - 参数;性能调优入门

tomcat版本号:8.5.82

Tomcat的实现在org.apache.catalina.core.StandardThreadExecutor 里面的参数有

点这里→_→ 查看官方文档
在这里插入图片描述

  • maxThreads - Tomcat线程池最多能起的线程数
  • maxConnections - Tomcat最多能并发处理的请求(连接)
  • acceptCount - Tomcat维护最大的对列数
  • minSpareThreads - Tomcat初始化的线程池大小或者说Tomcat初始化线程池最少会有这么多线程。

maxThreads VS maxConnections 的区别

  • maxThreads是指Tomcat线程池最多能起的线程数
  • maxConnections则是Tomcat一瞬间做多能够处理的并发连接数

eg:
      比如maxThreads=1000,maxConnections=800,假设某一瞬间的并发时1000,那么最终Tomcat的线程数将会是800,即同时处理800个请求,剩余200进入队列“排队”,如果acceptCount=100,那么有100个请求会被拒掉。

StandardThreadExecutor 类

public class StandardThreadExecutor extends LifecycleMBeanBase
        implements Executor, ResizableExecutor {

    protected static final StringManager sm = StringManager.getManager(StandardThreadExecutor.class);

    // ---------------------------------------------- Properties
    /**
     * Default thread priority
     * 默认线程的优先级
     */
    protected int threadPriority = Thread.NORM_PRIORITY;

    /**
     * Run threads in daemon or non-daemon state
     * 守护线程
     */
    protected boolean daemon = true;

    /**
     * Default name prefix for the thread name
     * 线程名称的前缀
     */
    protected String namePrefix = "tomcat-exec-";

    /**
     * max number of threads
     * 最大线程数默认200个
     */
    protected int maxThreads = 200;

    /**
     * min number of threads
     * 最小空闲线程25个
     */
    protected int minSpareThreads = 25;

    /**
     * idle time in milliseconds
     * 超时时间为6000毫秒
     */
    protected int maxIdleTime = 60000;

    /**
     * The executor we use for this component
     * 线程池容器
     */
    protected ThreadPoolExecutor executor = null;

    /**
     * the name of this thread pool
     * 线程池名称
     */
    protected String name;

    /**
     * The maximum number of elements that can queue up before we reject them
     * 队列最大限度值
     */
    protected int maxQueueSize = Integer.MAX_VALUE;

    /**
     * After a context is stopped, threads in the pool are renewed. To avoid
     * renewing all threads at the same time, this delay is observed between 2
     * threads being renewed.
     * 为了避免在上下文停止之后,所有的线程在同一时间段被更新,所以进行线程的延迟操作
     */
    protected long threadRenewalDelay =
        org.apache.tomcat.util.threads.Constants.DEFAULT_THREAD_RENEWAL_DELAY;

    //任务队列
    private TaskQueue taskqueue = null;
    // ---------------------------------------------- Constructors
    public StandardThreadExecutor() {
        //empty constructor for the digester
    }


    // ---------------------------------------------- Public Methods

    /**
     * Start the component and implement the requirements
     * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that prevents this component from being used
     */
    @Override
    protected void startInternal() throws LifecycleException {
        //1.实例化任务队列
        taskqueue = new TaskQueue(maxQueueSize);
        //自定义的线程池工厂类,实现了JDK的ThreadFactory接口
        TaskThreadFactory tf = new TaskThreadFactory(namePrefix,daemon,getThreadPriority());

       //这里的ThreadPoolExecutor是tomcat自定义的(并非是JDK所属的)
        executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS,taskqueue, tf);
        executor.setThreadRenewalDelay(threadRenewalDelay);
        //设置任务容器的父级线程池对象
        taskqueue.setParent(executor);

        //设置容器启动状态
        setState(LifecycleState.STARTING);
    }


    /**
     * 停止容器时的生命周期方法,关闭线程池和资源清理
     * Stop the component and implement the requirements
     * of {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that needs to be reported
     */
    @Override
    protected void stopInternal() throws LifecycleException {

        setState(LifecycleState.STOPPING);
        if (executor != null) {
            executor.shutdownNow();
        }
        executor = null;
        taskqueue = null;
    }


    /**
     * 这个执行线程方法有超时操作,参考 org.apache.catalina.Executor 接口
     * @param command the runnable task
     * @param timeout the length of time to wait for the task to complete
     * @param unit    the units in which timeout is expressed
     *
     */
    @Override
    @Deprecated
    public void execute(Runnable command, long timeout, TimeUnit unit) {
        if (executor != null) {
            executor.execute(command,timeout,unit);
        } else {
            throw new IllegalStateException(sm.getString("standardThreadExecutor.notStarted"));
        }
    }


    /**
     * JDK默认操作线程的方法,参考java.util.concurrent.Executor 接口
     * @param command the runnable task
     */
    @Override
    public void execute(Runnable command) {
        if (executor != null) {
            // Note any RejectedExecutionException due to the use of TaskQueue
            // will be handled by the o.a.t.u.threads.ThreadPoolExecutor
            executor.execute(command);
        } else {
            throw new IllegalStateException(sm.getString("standardThreadExecutor.notStarted"));
        }
    }
    
	... ... ...
	
	/**
     * 由于继承了 org.apache.tomcat.util.thread.ResizableExecutor 接口,所以可以重新定义线程池大小
     * @param corePoolSize 核心线程池大小
     * @param maximumPoolSize 最大核心线程池大小
     * @return
     */
    @Override
    public boolean resizePool(int corePoolSize, int maximumPoolSize) {
        if (executor == null) {
            return false;
        }

        executor.setCorePoolSize(corePoolSize);
        executor.setMaximumPoolSize(maximumPoolSize);
        return true;
    }
}

配置修改

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"  
       maxThreads="350" minSpareThreads="20" prestartminSpareThreads="true"/>  
<Connector executor="tomcatThreadPool" acceptCount="300000"  
               port="8080" protocol="HTTP/1.1"  
               connectionTimeout="20000"  
               redirectPort="8443" />  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值