Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the thread pool method used by springboot

Shulou Source: shulou.com Published: 2022-06-02 05:31:54 10月19日 Update

This article focuses on "what is the thread pool method used by springboot". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the thread pool method used by springboot"?

Thread pool creation @ Configuration@EnableAsyncpublic class TaskPoolConfig {@ Bean ("syncExecutorPool") public Executor taskExecutor () {ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor (); / / Core pool size taskExecutor.setCorePoolSize (5); / / maximum number of threads taskExecutor.setMaxPoolSize (10); / / queue level taskExecutor.setQueueCapacity (100) / / thread idle time taskExecutor.setKeepAliveSeconds (60); / / thread prefix name taskExecutor.setThreadNamePrefix ("syncExecutor--") / / this method is used to set when thread pool is closed, wait for all tasks to be completed, and then continue to destroy other Bean, / / so that the destruction of these asynchronous tasks will precede the destruction of database connection pool objects. TaskExecutor.setWaitForTasksToCompleteOnShutdown (true); / / the wait time of a task is forced to be destroyed if it is not destroyed beyond this time to ensure that the application can eventually be closed rather than blocked. TaskExecutor.setAwaitTerminationSeconds (60); / / when the thread is insufficient, the task is handled by the calling thread taskExecutor.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ()); return taskExecutor;}} thread pool parameters

Number of core threads: CorePoolSize

Look at the business scenario settings (I usually set it to CPU core * 1)

Maximum number of threads: MaxPoolSize

When the number of core threads is in use, the threads in the maximum number of threads will be called, the total number of threads used will not exceed the maximum number of threads, and the rest will wait (I usually set it to CPU core * 2)

Queue level: QueueCapacity

The size of the thread queue

Thread idle time: KeepAliveSeconds

Thread prefix name: ThreadNamePrefix

Downtime strategy: WaitForTasksToCompleteOnShutdown

This method is used to set that when the thread pool is closed, wait for all tasks to be completed, and then continue to destroy other Bean, so that the destruction of these asynchronous tasks will precede the destruction of database connection pool objects.

Wait time for the task: AwaitTerminationSeconds

If the waiting time of the task exceeds this time, it is forced to destroy before it is destroyed, so as to ensure that the application can eventually be closed instead of blocked.

Reject policy: RejectedExecutionHandler

When there is not enough thread, the calling thread handles the task.

Thread Pool Test 1 (number of core threads)

Result: the number of core threads in the thread pool is performed in rotation.

Thread pool test 2 (when the number of core threads and the maximum number of threads are insufficient)

Random sleep 0-10000 milliseconds

New Random (). NextInt (10000)

Results:

When the number of 5 core threads is insufficient, use the threads in the maximum number of threads

When the maximum number of threads is insufficient, use the main thread to perform the task

/ / the task is handled by the calling thread when there is not enough thread

TaskExecutor.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ())

At this point, I believe you have a deeper understanding of "what is the thread pool method used by springboot". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Thread quantity task core maximum time method insufficient queue processing content prefix name size object data database time degree idle Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia Shulou Tech Info Xiaomi Redmi