哈喽,大家好,我是了不起。

高考结束了,依然祝高考学子们取得好成绩,然后报考计算机专业,然后快点毕业,来接收哥哥姐姐们留下的代码文明。
接到某城举报,某迪泄露高考题目。
最近生产环境各种问题,几百台设备停摆,真的是亚历山大。接手了别人的代码,只能硬着头皮改了,一不留神就恰班到10点多了。
今天还是说一下线程池的两个思考。
我们常用的线程池,
1. JDK的ThreadPoolExecutor.
2. CompletableFutures 默认使用了ForkJoinPool.commonPool()。
CompletableFuture.supplyAsync(()->{ return "hello word";});3. 还有Tomcat中的线程池
org.apache.tomcat.util.threads.TaskQueue
org.apache.tomcat.util.threads.ThreadPoolExecutor
线程池维护多个线程,等待监督管理者分配可并发执行的任务。这种做法,一方面避免了处理任务时创建销毁线程开销的代价,另一方面避免了线程数量膨胀导致的过分调度问题,保证了对内核的充分利用。
public ThreadPoolExecutor(
    int corePoolSize, //核心线程数
    int maximumPoolSize,//最大线程数
    long keepAliveTime, //大于核心线程数量的线程存活时间,如果没有新任务就会关闭
    TimeUnit unit, // 时间单位
    BlockingQueue workQueue, //线程等待队列
    ThreadFactory threadFactory,//创建线程的工厂
    RejectedExecutionHandler handler//拒绝策略
) { JDK线程池执行任务:
这里需要注意直接使用LinkedBlockingQueue阻塞队列作为线程池会存在一个问题,当workcount > corePool时优先进入队列排队, 当请求并发过多时会导致请求缓慢,队列太长可能会出现内存溢出(先排队再涨线程池)
下面时Tomcat线程池的构造方法
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
    this.ctl = new AtomicInteger(ctlOf(-536870912, 0));
    this.mainLock = new ReentrantLock();
    this.workers = new HashSet();
    this.termination = this.mainLock.newCondition();
    this.submittedCount = new AtomicInteger(0);
    this.lastContextStoppedTime = new AtomicLong(0L);
    this.lastTimeThreadKilledItself = new AtomicLong(0L);
    this.threadRenewalDelay = 1000L;
    if (corePoolSize >= 0 && maximumPoolSize > 0 && maximumPoolSize >= corePoolSize && keepAliveTime >= 0L) {
        if (workQueue != null && threadFactory != null && handler != null) {
            this.corePoolSize = corePoolSize;
            this.maximumPoolSize = maximumPoolSize;
            this.workQueue = workQueue;
            this.keepAliveTime = unit.toNanos(keepAliveTime);
            this.threadFactory = threadFactory;
            this.handler = handler;
            this.prestartAllCoreThreads();
        } else {
            throw new NullPointerException();
        }
    } else {
        throw new IllegalArgumentException();
    }
} Tomcat主要针对web接口请求,不能因为LinkedBlockingQueue的排队导致接口出现大量延迟和缓慢, 从而使用了tomcat的TaskQueue,TaskQueue继承了JDK的LinkedBlockingQueue 并扩展了JDK线程池的功能。
主要有一下几点优化:
public void execute(Runnable command, long timeout, TimeUnit unit) {
    this.submittedCount.incrementAndGet();
    try {
        this.executeInternal(command);
    } catch (RejectedExecutionException var9) {
        if (!(this.getQueue() instanceof TaskQueue)) {
            this.submittedCount.decrementAndGet();
            throw var9;
        }
        TaskQueue queue = (TaskQueue)this.getQueue();
        try {
            if (!queue.force(command, timeout, unit)) {
                this.submittedCount.decrementAndGet();
                throw new RejectedExecutionException(sm.getString("threadPoolExecutor.queueFull"));
            }
        } catch (InterruptedException var8) {
            this.submittedCount.decrementAndGet();
            throw new RejectedExecutionException(var8);
        }
    }
}
我们看看AI如何回复
了不起认为大多数情况下使用JDK的线程池就够用了,如果觉得线程数据处理不过来,需要多一点线程直接增加核心线程数量设置就可以了。针对资源比较紧张,对线程使用代价比较高时可以考虑。
tomcat对线程池做过优化,也必然是有一定的考量,对于线程资源的使用频率比较高的情况下可以使用。
了不起之前也有对应的不同的例子来说明不同点。这里再放出来让大家体会一下!
JDK线程池,线程资源较珍贵
Tomcat线程池,线程资源使用比较频繁
                文章标题:珍贵的线程资源,JDKVSTomcat
                
                本文来源:http://www.csdahua.cn/qtweb/news14/445814.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网