In the previous article "Creating a java thread (focus: using a thread pool thread pool is not allowed to create Executors)" in the configuration of the thread pool,
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler) {
......
}
Buffer queue queue handler and exception handling strategies have been described, but there are two important parameters: the core number of threads corePoolSize, the maximum number of threads maximumPoolSize . Therefore, to determine the number of threads is also very important, here is my own understanding of access to information and results
Threads and what factors?
1. CPU
In the beginning introduce multi-threaded "multi-threaded from shallow and deep" when introduced to the context of the threads share the process, a more fine-grained CPU time . Therefore, to determine the number of threads and CPU related. As for the relationship between the number of cores and threads of the CPU, you can view this article: number of CPU cores, the relationship and the difference between the number of threads . (Parallel multi-threaded actually use a variety of computer resources, there are a few core CPU with no relationship)
2. IO
IO IO into the disk and network IO. The key factors affect disk is a disk service time, i.e., a time to complete the disk I / O request by the time, which consists of seek time, rotational latency, and data transfer time into three parts. A measure of its key indicators, roughly IOPS, throughput and so on. The key factors affecting the network of the server IO latency bandwidth limitations + + + jump routing network latency delay + receiving delay local.
3. Parallel
Examples of multiple cpu or simultaneously performed for a plurality of machines processing logic
4. Concurrent
CPU constantly switching threads to be multiplexed to improve efficiency. By cpu scheduling algorithm looks while performing, in fact, from the operational level cpu is not true at the same time. Usually use QPS TPS or processing capacity to the reaction system
Nature of the tasks
1. CPU-intensive tasks
Large number of calculations to be performed, CPU resource consumption, such as calculation of PI, high-definition video decoding and the like, thanks to the computing capacity of the CPU. To most efficiently a CPU, a number of computationally intensive tasks simultaneously should be equal to the number of CPU cores.
General Configuration = total number of +1 the CPU core threads (+1 idle waiting to use)
2. IO-intensive tasks
CPU consumption of these tasks, and most of the time a task is waiting for IO operation is complete (because of IO rate is far below the CPU and memory speed). The most common tasks are IO-intensive tasks, such as Web applications. For IO intensive tasks, the more the task, the higher the CPU efficiency (but limited).
General configuration of the number of threads = number of total core CPU * 2 +1
to sum up
According to concurrent programming network "How to reasonably estimate the thread pool size" in an article tips,
= Optimal number of threads (threads with a thread CPU latency time ratio + 1) * Number of CPU
So threads waiting time occupied the higher the ratio, the more need for multi-threading. Thread CPU time occupied the higher the ratio, the less need thread
problem
1. Did you use the thread pool will certainly be higher than the single-threaded efficient?
no. Such as Redis (Click to view) .
2. A network of concurrent programming problem:
2.1 high concurrency, short of task execution time business how to use thread pool?
The number of thread pool threads may be provided as the number of +1 CPU core, reducing the thread context switching
2.2 How concurrency is not high, long-time business using task execution thread pool?
a) If the business is a long time focused on the IO operation, that is IO-intensive task, because the operation does not take up CPU IO, so do not let all the CPU retired and sit, you can increase the number of threads in the pool, let CPU processing more business
b) If the traffic is concentrated in a long time operation is calculated, which is computationally intensive tasks, the number of threads in the pool is set to be less, reducing the thread context switching
2.3 high concurrency, how long time business execution services using a thread pool?
The key high concurrency, long-time business execution, to address this type of task is not thread pool but in the overall architecture design, to see whether they can do business inside some of the data cache is the first step, the second step is to add servers, and set the thread pool. Finally, a long time business execution problems, may also need to analyze it, see if you can use the middleware to split tasks and decoupling.
reference:
"How reasonable set thread pool size"
"Relations with the IO and CPU threads"
"Liao Xuefeng: python processes vs threads"
在上一篇文章《java线程的创建(重点:线程池的使用,线程池不允许使用Executors创建)》中有关线程池的配置中,