Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure Asynchronous Thread Pool in Springboot

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Springboot asynchronous thread pool how to configure, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Configure thread pool

First we need to write a @ EnableAsync-enabled thread pool configuration class

Import java.util.concurrent.Executor

Import java.util.concurrent.ThreadPoolExecutor

Import org.springframework.context.annotation.Bean

Import org.springframework.context.annotation.Configuration

Import org.springframework.scheduling.annotation.EnableAsync

Import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor

/ / start async

@ EnableAsync

/ / this is a configuration class

@ Configuration

Class TaskPoolConfig {

/ / if the name of Bean is not set, there is no way to correspond to the configuration information in the task.

@ Bean ("taskExecutor")

Public Executor taskExecutor () {

/ / create thread pool based on ThreadPoolTaskExecutor

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ()

/ / set the initial number of threads for threads 5 threads

Executor.setCorePoolSize (5)

/ / set the maximum number of threads for threads: 10 threads

Executor.setMaxPoolSize (10)

/ / set the maximum number of tasks for the task queue

Executor.setQueueCapacity (200)

/ / set the existence time beyond the initialization thread to 60 seconds.

/ / that is, if the number of existing threads exceeds 5, the destroy time will be set to 60 seconds for the excess idle threads.

Executor.setKeepAliveSeconds (60)

/ / set thread prefix

Executor.setThreadNamePrefix ("taskExecutor-")

/ / saturation policy of thread pool what I set here is CallerRunsPolicy, that is, there are four types of tasks executed by the thread in which the caller is located.

/ / AbortPolicy: throw an exception directly, which is the default policy

/ / CallerRunsPolicy: use the caller's thread to execute the task

/ / DiscardOldestPolicy: discard the first task in the blocking queue and execute the current task

/ / DiscardPolicy: discard the task directly

Executor.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ())

/ / sets whether to wait for the task to complete when closing the thread pool

Executor.setWaitForTasksToCompleteOnShutdown (true)

/ / set the number of seconds to wait for termination

Executor.setAwaitTerminationSeconds (60)

/ / return the thread pool that has been set up

Return executor

}

}

Classes that use thread pools

When using thread pools, we need to add @ Async annotations to the task methods that use thread pools.

Import lombok.extern.slf4j.Slf4j

Import org.springframework.scheduling.annotation.Async

Import org.springframework.stereotype.Component

/ / enable log to output information in the console

@ Slf4j

/ / enable the @ Component annotation to inject this class into the spring container

@ Component

Public class test {

/ / set the configuration class for asynchronous invocation for Hello class methods

@ Async ("taskExecutor")

Public void Hello (String hello) throws InterruptedException {

/ / start the task

Log.info ("Task start and extend execution time")

/ / delay execution

Thread.sleep (1000)

/ / execute output

Log.info (hello)

}

}

Test thread pool

The method of writing a test thread pool that leverages the unit tests of idea

Import org.junit.Test

Import org.junit.runner.RunWith

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.boot.test.context.SpringBootTest

Import org.springframework.test.context.junit4.SpringRunner

@ RunWith (SpringRunner.class)

@ SpringBootTest

Public class ApplicationTests {

/ / automatic assembly thread test class

@ Autowired

Private test test

@ Test

Public void test () throws Exception {

/ / run 30 tasks in a cycle

For (int I = 0 position I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report