Logical core number

General Baidu how to obtain the computer’s logical core number, will appear this code:

Runtime.getRuntime().availableProcessors()
Copy the code

The problem, however, is that runtime.getruntime ().availableProcessors() don’t always return the number you want either. For example, on my dual-core 1-2-1 machine, it returned a 2, which is correct. But on my 1-4-2 (1 physical processor, 4 cores, 2 hyperthreads per core = commonly known as 4 cores, 8 threads per core) machine, one CPU slot, 4 cores, 2 hyperthreads per core, would return 8. But I only have four cores, and if the bottleneck of the code was in the CPU, I would have seven threads competing for CPU cycles at the same time, instead of the more reasonable four threads. If my bottleneck is in memory, I can get a sevenfold performance improvement from this test.

But that’s not all! A buddy at Java Champions found a situation where he had a 16-4-2 machine (16 CPU slots, 4 cores per CPU, 2 hyperthreads per core, and returned 16! From the results on my i7 Macbook Pro, I think I should return 1642=128. Running Java 8 on this machine will only set the concurrency of the universal FJ pool to 15. As Brian Goetz points out, “The virtual machine doesn’t really know what a processor is, it just asks the operating system to return a value. Similarly, the operating system does not know what is going on, it is the hardware device to ask. The hardware will tell it a value, usually the number of hardware threads. The operating system believes what the hardware says and the virtual machine believes what the operating system says.”

Therefore, different services have different situations. Therefore, thread pools cannot be uniformly configured based on the number of cores.

So if our thread pool is mainly used to deal with IO intensive tasks, such as reading files, database connection, network communication, etc., we all know that IO read and write speed, compared with the CPU, must be much slower, so it is generally recommended to go to the machine logic core number twice.

Multiple thread pools

A common project may create multiple thread pools, because for example, two kinds of business, one is not high requirement, but the number is very large, can retry, the other is small, timely requirements, if put together, because the number of large may affect your small sum,

Thread pools are used to manage threads. Generally, there is a timing thread pool, a service thread pool, and an IO thread pool. Sometimes, the timing thread pool and the service thread pool can also be shared, such as the Netty thread pool. Business thread pools are used to process core services and generally require high speed and low latency, while I/O thread pools are used for network interactions such as database operations, RPC requests, or disk reads and writes.

The business scenario

The main thing to look at first is the nature of the tasks our thread pool is used to perform. If the thread pool is primarily used for computationally intensive tasks, such as encryption, large number decomposition and other cpu-intensive tasks, it is generally recommended that our thread count be set to the number of cores (logical cores) of the machine + 1

Why do we add one here? This is to prevent our CPU from having a page miss situation (page miss is when the data needed for the thread’s execution has not been loaded yet, and some of it is still on disk, so the thread will be suspended).


So if our thread pool is mainly used to deal with IO intensive tasks, such as reading files, database connection, network communication, etc., we all know that IO read and write speed, compared with the CPU, must be much slower, so it is generally recommended to go to the machine logic core number twice.

And if we have a thread pool that handles both computationally intensive and IO intensive tasks. So how do we deal with it?

IO intensive takes much more time than computing intensive, so it is not necessary to split. But if IO intensive takes about the same amount of time as computation intensive, it is recommended to split it up.