“This is the 34th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

1. The introduction

Like me, you may have seen this theory on the Internet (whether it’s true or not) :

  • Compute-intensive: number of CPU cores +1
  • I/O intensive: Number of CPU cores x 2

Linux Query command: lscpu

According to the above theory, my thread pool size should look like this:

  • Compute-intensive: Number of CPU cores +1=17
  • I/O intensive: Number of CPU cores x 2=32

If there are multiple thread pools in a project, should each pool be set to the same number of threads? Here are a few questions:

2. Can there be only one thread pool in a project?

Can there be only one thread pool for a project? This problem, obviously everyone knows it’s not possible to have only one thread pool. For a simple Web project, there will be at least one Tomcat thread pool. If the project needs to connect to a database, there is at least one link management thread pool for the database. So there are at least two thread pools. Conclusion: Most projects have multiple thread pools, especially Web projects, but some non-Web applications may have only one thread pool or none at all.

In the case of multiple thread pools, the theory mentioned above does not apply, because the default size of tomcat thread pool in Web projects is 200, which is obviously not correct. So how to set the size of the thread pool (how many threads in the thread pool) first to understand the following questions:

  • Is there a difference between a single thread pool and how threads execute in a thread pool

    For example, creating 10 separate threads to start running is not the same as creating a pool of 10 threads to get CPU execution

  • Are two pools with 5 threads the same as one pool with 10 threads

With these two questions, let’s examine the second question in the figure above in code: how does the CPU execute threads when multiple thread pools coexist

3. How does the CPU execute threads when multiple thread pools coexist

Consider the following three code examples

3.1 Four single threads

public class ThreadNumTest {
    public static void main(String[] args) {
        for(int i = 0; i < 4; ++i){
            new Thread(new Runnable() {
                @Override
                public void run(a) {
                    do{}while (true); }},"mxsm").start();
        }
        System.out.println("Main is finished."); }}Copy the code

Compile and run on the server:

Then run the following command to observe:

$ jps
$Top-h-p < Java program PID>
Copy the code

After running the preceding command, press number key 1 to view the CPU usage

There are four cpus that have reached 100% utilization. Now let’s do case number two

3.2 One thread pool -4 threads:

public class ThreadNumTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        for(int i = 0; i < 4; ++i){
            executorService.submit(new Runnable() {
                @Override
                public void run(a) {
                    do{}while (true); }}); } System.out.println("End of main function run - one thread pool -4 threads"); }}Copy the code

Compile and run on the server:

View by command (the first case above)

We found four cpus that were 100% utilized, and then we looked at the third case

3.3 2 Thread pools with 2 threads:

public class ThreadNumTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        ExecutorService executorService1 = Executors.newFixedThreadPool(2);
        for(int i = 0; i < 2; ++i){
            executorService.submit(new Runnable() {
                @Override
                public void run(a) {
                    do{}while (true); }}); executorService1.submit(new Runnable() {
                @Override
                public void run(a) {
                    do{}while (true); }}); } System.out.println("Main finished -2 thread pools 2 threads each"); }}Copy the code

Compile and run on the server:

View by command (the first case above)

Four cpus were found to have reached 100% utilization.


A conclusion can be drawn from the code demonstration: the number of thread pools and whether to pool has no effect on the EXECUTION of CPU threads. Again, thread pooling only affects efficiency and does not affect the CPU’s ability to execute the underlying logic on threads. (By efficiency, threads need to execute tasks without pooling, create new tasks, and destroy tasks after completion. Efficiency is affected here). All four cpus are 100 percent utilization.

In most cases, the program will have I/O operations, or data packets sent and received by the network. These operations require waiting feedback, in which the thread is in a waiting state, the CPU becomes inactive, and the operating system dispatches the CPU to execute other threads. This is where the context switch occurs.

4. The Load Load averages the process context switch

Before I talk about setting the number of threads, let’s talk about the Load Load in Linux,

4.1 System Load Averages in Linux

View commands:

uptime
#or
top
Copy the code

Understanding Linux CPU Load Here’s a picture in the article:

The figure above depicts CPU overload:

  • Are processes that use CPU time slices (” Bridges “) or queue to use CPU
  • A channel represents a CPU
  • Run queue length: The total number of processes currently running plus the number of processes waiting (queued) to run

Understand a few important points:

  • The Load Averages are in the range of 0.00-1 (excluding 1) and the CPU averages are operating normally this is a preferable condition and the vehicle on the bridge is not congested and does not reach maximum endurance
  • The Load Averages a value of 1, indicating that the CPU is operating at full capacity and the vehicle on the bridge is not congested and averages the maximum Load
  • The Load Averages value greater than 1 indicates that the CPU is overloaded and the car on the bridge has reached its maximum capacity and starts to averages a traffic jam at the entrance to the bridge

Tips:

  1. In the case of single-core single-thread averages, the Load averages is not 1. For example, MY CURRENT CPU8-core 16-thread Load averages=16 is considered to be running at full capacity.
  2. The Load Averages =1 does not have any other space and averages around 0.7. If it is 1.0 you run your computer and run out of time because the CPU is only running at full capacity. For other extra tasks, you need to queue, which can cause your computer to stall.

4.2 Process context switch

For a process to perform a task, if the process is bound to a CPU, the CPU will execute only that process, in which case there is no context switch for the process. The context switch of the CPU is a waste of time. So too many context switches can also slow things down. The same task can be performed 100 times without a context switch than with a context switch.

Context switching classification:

  • Voluntary context switch

    Context switch when a process is unable to obtain required resources. For example, voluntary context switching occurs when system resources such as I/O and memory are insufficient

  • Involuntary context switching

    Context switch occurs when a process is forcibly scheduled by the system due to the time slice

Difference between Thread vs Process in Java? And What is Distinguishing Between Java Threads and OS Threads? Here’s the answer.

5. Set the number of threads

The general rule: When the CPU is full (or 0.7 at full load or some fixed value), adjusting the number of threads allows the system to accomplish as many tasks as possible per unit of time

The number of threads is determined by the number of times that a task can be completed as many times as possible in a unit time in the appropriate number of threads. Here, the number of threads refers to the threads that execute a task with a high frequency, for example: The Tomcat execution thread pool, the database thread pool, etc., and getting a configured thread pool or thread pool every hour is not a consideration.


Thread count Settings will be covered in the next article.

I am ant back elephant, the article is helpful to you like to pay attention to me, the article has incorrect place please give correct comments ~ thank you

Reference Documents:

  • Difference between Thread vs Process in Java?
  • Distinguishing between Java threads and OS threads?
  • Understanding Linux CPU Load