Tomcat的实现在org.apache.catalina.core.StandardThreadExecutor 里面的参数有
点这里→_→ 查看官方文档
maxThreads VS maxConnections 的区别
eg:
比如maxThreads=1000,maxConnections=800,假设某一瞬间的并发时1000,那么最终Tomcat的线程数将会是800,即同时处理800个请求,剩余200进入队列“排队”,如果acceptCount=100,那么有100个请求会被拒掉。
public class StandardThreadExecutor extends LifecycleMBeanBaseimplements 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;// ---------------------------------------------- Constructorspublic 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*/@Overrideprotected 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*/@Overrideprotected 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@Deprecatedpublic 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*/@Overridepublic 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.ThreadPoolExecutorexecutor.execute(command);} else {throw new IllegalStateException(sm.getString("standardThreadExecutor.notStarted"));}}... ... .../*** 由于继承了 org.apache.tomcat.util.thread.ResizableExecutor 接口,所以可以重新定义线程池大小* @param corePoolSize 核心线程池大小* @param maximumPoolSize 最大核心线程池大小* @return*/@Overridepublic boolean resizePool(int corePoolSize, int maximumPoolSize) {if (executor == null) {return false;}executor.setCorePoolSize(corePoolSize);executor.setMaximumPoolSize(maximumPoolSize);return true;}
}
上一篇:Java基础算法题