首页  

tomcat线程池要点     所属分类 tomcat 浏览量 1160
org.apache.tomcat.util.threads.ThreadPoolExecutor

public class ThreadPoolExecutor extends java.util.concurrent.ThreadPoolExecutor


任务队列 使用 
org.apache.tomcat.util.threads.TaskQueue



public class TaskQueue extends LinkedBlockingQueue<Runnable>

注意重写了 offer  方法

    @Override
    public boolean offer(Runnable o) {
      //we can't do any checks
        if (parent==null) return super.offer(o);
        //we are maxed out on threads, simply queue the object
        if (parent.getPoolSize() == parent.getMaximumPoolSize()) return super.offer(o);
        //we have idle threads, just add it to the queue
        if (parent.getSubmittedCount()<=(parent.getPoolSize())) return super.offer(o);
        //if we have less threads than maximum force creation of a new thread
        if (parent.getPoolSize()<parent.getMaximumPoolSize()) return false;
        //if we reached here, we need to add it to the queue
        return super.offer(o);
    }
    

    
 
线程数小于maximumPoolSize,返回false,创建新线程执行任务,

jdk线程池 使用LinkedBlockingQueue 默认capacity为Integer.MAX_VALUE ,maxThreads无效。
可使用同步队列,解决该问题 ,因为 同步队列 容量为 0 

通过重写 offer , 改变 线程池扩容策略

先扩容线程池 再 放入任务队列 

jdk 线程池 默认的线程池扩容策略为  任务队列满了再扩容线程池

上一篇     下一篇
ThreadPoolExecutor中的ctl变量

2019年一季度A股投资者结构

java线程池实例

java线程池系列文章汇总

tomcat参数 acceptCount maxConnections maxThreads

Java中的整数缓存IntegerCache