SpringBoot uses multithreading to perform timing tasks
I use a timer to perform some operations in a Spring Boot project, such as sending data once every 10 seconds.
There are 2 of these operations, as shown below.
I just thought, although these two operations specify the time frequency, if one of them is very time-consuming, will it affect the other operations?
The answer is yes.
@Service public class ShareDataBySend { @Autowired SendDataService sendDataService; //Operation 1 @Scheduled(fixedRateString = "${}") // Execute every * seconds private void send() { (); } //Operation 2 @Scheduled(fixedRateString = "${}") // Execute every * seconds private void send2() { (); } }
What to do? AI tells me to introduce multithreading.
Introducing multi-threading
In the above code, the annotation @Scheduled is used. This annotation tells Spring that it needs to execute the annotation method regularly. @Scheduled relies on Spring's task scheduling mechanism,Use a single thread by defaultThe task scheduler executes tasks. If the thread pool is not explicitly configured, all timing tasks will be executed sequentially in the same thread. However, when we configure a thread pool task scheduler, Spring willautomaticUse this scheduler so that each timing task (such as send() and send2()) will be executed concurrently in different threads.
In other words, we do not need to modify the ShareDataBySend class above, but register a new thread pool task scheduler, and the system will automatically switch to multi-threading. All of this comes from the Spring Boot framework's own mechanism.
Code Example
1. Added thread pool task scheduler configuration class
import ; import ; import ; import ; @Configuration public class SchedulerConfig implements SchedulingConfigurer { /** * We replaced the default single-threaded scheduler with ThreadPoolTaskScheduler and set the thread pool size (e.g. 10). * This means up to 10 tasks can be executed concurrently. */ @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); (10); // Set the thread pool size and adjust it as needed (); (taskScheduler); } }
When the system starts, Spring Boot will instantiate the type with the @Config annotation into the container, so the ThreadPoolTaskScheduler configured by SchedulerConfig is initialized.
When a timed task is triggered (as defined by @Scheduled), the task is submitted to ThreadPoolTaskScheduler for processing.
If there are multiple tasks, they are assigned to execute on different threads in the thread pool, thus achieving concurrency.
2. The original ShareDataBySend class
No modification is required
Summarize
I have been using Java for several years. Actually, I have always used Spring Boot. Spring Boot is a java development framework, but I feel that Spring Boot is excellent and convenient enough. For me, Spring Boot == Java. Of course, in essence, Spring Boot can be regarded as a genre of J2EE, but it is better than the best.
In addition, I asked Tongyi Qianwen and chatGPT respectively for the same questions. The solutions given by the former are not only cumbersome, but also have errors. This may not be a problem with the model, but a problem with the material for the model training. In other words, perhaps in the Chinese world, the quality of answers to programming questions is not in the same level as that of foreigners.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.