简体   繁体   English

线程池任务执行器

[英]Thread Pool Task Executor

Why Spring's ThreadPoolTaskExecutor keep on creating threads up to Core Size value, no matter existing threads are idle!为什么无论现有线程处于空闲状态,Spring 的 ThreadPoolTaskExecutor 都会继续创建达到 Core Size 值的线程!

    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(300);
    executor.setMaxPoolSize(500);
    executor.setQueueCapacity(5000);
    executor.setThreadNamePrefix("AsyncTask-");
    executor.initialize();

I fired requests one by one and it kept on increasing the number of thread until it reached 300. My question is if an existing thread is idle, why it isn't using the idle one?我一一发出请求,它不断增加线程数,直到达到 300。我的问题是,如果现有线程空闲,为什么它不使用空闲线程? Once the Core Pool size is reached its anyway using the threads from pool only.一旦达到核心池大小,无论如何都只使用池中的线程。

在此处输入图像描述

Spring has less to do here as per java documentation here根据此处的 java 文档,Spring 的工作量较少

When a new task is submitted in method execute(Runnable), and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle.当在方法 execute(Runnable) 中提交了一个新任务,并且运行的线程少于 corePoolSize 时,即使其他工作线程处于空闲状态,也会创建一个新线程来处理该请求。 If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full如果运行的线程数多于 corePoolSize 但少于 maximumPoolSize,则仅当队列已满时才会创建新线程

Core thread pool size specifies the number of threads that are to be kept ready to handle any potential work to avoid the overhead of creating new threads.核心线程池大小指定准备好处理任何潜在工作的线程数,以避免创建新线程的开销。 Specifying 300 is asking for a threadpool that maintains 300 threads at all time, you shouldn't expect it to reuse before the number is met.指定 300 是要求一个始终维护 300 个线程的线程池,您不应该期望它在满足数量之前重用。 If this is too high, consider decreasing the corePoolSize while maintaining the same maxPoolSize, it scales up when the pool is overloaded.如果这太高,请考虑在保持相同 maxPoolSize 的同时减小 corePoolSize,当池过载时它会向上扩展。 Check out ThreadPoolTaskExecutor's setAllowCoreThreadTimeOut to scale down the thread pool below corePoolSize when not in use.查看 ThreadPoolTaskExecutor 的setAllowCoreThreadTimeOut以在不使用时缩小 corePoolSize 以下的线程池。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM