Location>code7788 >text

Tomcat thread pool in detail, why SpringBoot supports up to 200 concurrency?

Popularity:94 ℃/2024-10-22 07:58:14

Q: Classic interview question, How many requests can a SpringBoot application process concurrently at the same time?

A: SpringBoot application concurrent processing request number is mainly affected by two factors, the use of Servlet container (the default use of Tomcat, commonly used and jetty, undertow) and configuration items. So in the default configuration, SprigBoot application can concurrently process 200 requests.

SprigBoot uses Tomcat by default, and the maximum number of threads in the Tomcat thread pool is 200. Here some of you have questions, shouldn't the number of concurrent threads be affected by the length of the queue first, and is the maximum number of threads used only if the length of the queue is also only 200?

Tomcat Thread Pool

The JDK's thread pool is configured using the number of core threads first, followed by the queue length, and then finally the maximum thread configuration.

Tomcat's thread pool, that is, first use the core thread count configuration, then use the maximum thread configuration, and only finally use the queue length.

underlying source code

runWorker

This part of the code looks familiar once you get to the runWorker:

#run

Inside the getTask method, you can see several key parameters about the thread pool:

#getTask

  • corePoolSize, the number of core threads, the value is 10.

  • maximumPoolSize, the maximum number of threads, the value is 200.

And based on the maximumPoolSize parameter, going back in the code, you'll see that the default value is 200:

The default queue length for the Tomcat thread pool:

Tomcat thread pool:

  • The number of core threads, with a value of 10.

  • Maximum number of threads, value is 200.

  • The length of the queue, the value is Integer.MAX_VALUE.

The execute method is executed when a task is submitted to the thread pool:

#execute()

For Tomcat it will call the executeInternal method:

#executeInternal

Inside this method, labeled

  • The place where ① is to determine whether the current number of worker threads is less than the number of core threads, if it is less, then directly call the addWorker method to create a thread.

  • The main point of ② is to call the offer method to see if more tasks can be added to the queue.

  • If you can't continue to add, the queue is full, so come to the place labeled ③ and see if you can execute the addWorker method to create non-core threads, i.e., enable the maximum number of threads.

Next, look at the (command) logic. If it returns true, it adds to the queue, and if it returns false, it enables the maximum number of threads.

The workQueue is a TaskQueue, which is a queue based on Tomcat's own LinkedBlockingQueue.

#offer

The place labeled ① determines whether the parent is null, and if it is, the offer method of the parent class is called directly. To enable this logic, our parent must not be null.

parent is the Tomcat thread pool, and its set method tells you that it is assigned a value after the thread pool is initialized. That is to say, in the Tomcat scenario, parent will not be empty.

Where ② is marked, the getPoolSizeNoLock method is called: this method gets how many threads are in the current pool. So if this expression is true:

() == ()

This means that the number of threads in the current thread pool is already the maximum number of threads configured, so call the offer method to put the current request into the queue.

The place labeled ③ is to determine whether the number of tasks that have been submitted to the thread pool to be executed or are being executed is less than the number of threads in the current thread pool. If so, it means that the current thread pool has free threads to execute the task, then put the task into the queue to go, it will be free threads to take away the execution.

The place labeled ④. Returns false if the current thread pool has fewer threads than the maximum number of threads configured for the pool. what happens when the offer method returns false?

If the offer returns false, then you start going to the place labeled ③ in the above figure to try to add non-core threads, i.e., to enable the maximum number of threads configuration.

summarize

The JDK's thread pool is configured using the number of core threads first, followed by the queue length, and then finally the maximum thread configuration.

Tomcat's thread pool, that is, first use the core thread count configuration, then use the maximum thread configuration, and only finally use the queue length.

So how to adjust the maximum number of threads to increase SpringBoot? See here should have understood, you can adjust the maximum number of threads to control the number of concurrency

Interview questions column

Java interview questions columnIt's online, so feel free to visit.

  • If you don't know how to write a resume, resume projects don't know how to package them;
  • If there's something on your resume that you're not sure if you should put on it or not;
  • If there are some comprehensive questions you don't know how to answer;

Then feel free to private message me and I will help you in any way I can.