How springBoot @ Scheduled enables multiple tasks to start execution at the same time
SpringBoot @ Scheduled how to implement multiple tasks at the same time, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
@ Scheduled starts executing multiple tasks at the same time simply by adding the
The code is as follows:
@ Bean public TaskScheduler taskScheduler () {ThreadPoolTaskScheduler taskExecutor = new ThreadPoolTaskScheduler (); taskExecutor.setPoolSize (50); return taskExecutor;}
@ Scheduled multiple timed tasks, overlapping execution @ Scheduled if there are two scheduled tasks
When scheduled tasks are repeated, only one can be executed.
As follows
Import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component; import java.time.LocalDateTime; @ Componentpublic class MyScheduled {@ Scheduled (cron = "0ram 5 *?") Public void execute1 () {String curName = Thread.currentThread (). GetName (); System.out.println ("current time: + LocalDateTime.now () +" task execute1 corresponding to thread name: "+ curName); try {Thread.sleep (1000);} catch (Exception e) {e.printStackTrace () } @ Scheduled (cron = "0pe5 *?") Public void execute2 () {String curName = Thread.currentThread (). GetName (); System.out.println ("current time: + LocalDateTime.now () +" task execute2 corresponding to thread name: "+ curName); try {Thread.sleep (1000);} catch (InterruptedException e) {e.printStackTrace ();}
As you can see from the execution, the print thread name is the same. That is, if the thread pool is not manually specified, a single thread is started by default to execute scheduled tasks.
If you want multiple scheduled tasks to overlap
You need to specify the thread pool manually, as follows
Import org.springframework.context.annotation.Bean;import org.springframework.scheduling.TaskScheduler;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;import org.springframework.stereotype.Component; import java.time.LocalDateTime; @ Component@EnableSchedulingpublic class MyScheduled {@ Bean public TaskScheduler taskScheduler () {ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler (); taskScheduler.setPoolSize (50); return taskScheduler } @ Scheduled (cron = "0ram 5 *?") Public void execute1 () {String curName = Thread.currentThread (). GetName (); System.out.println ("current time: + LocalDateTime.now () +" task execute1 corresponding to thread name: "+ curName); try {Thread.sleep (1000);} catch (Exception e) {e.printStackTrace () } @ Scheduled (cron = "0pe5 *?") Public void execute2 () {String curName = Thread.currentThread (). GetName (); System.out.println ("current time: + LocalDateTime.now () +" task execute2 corresponding to thread name: "+ curName); try {Thread.sleep (1000);} catch (InterruptedException e) {e.printStackTrace ();}
At this point, multiple scheduled tasks are executed by impassable threads, and at the same time, scheduled tasks can be executed overlapping.
This is the answer to the question about how springBoot @ Scheduled can implement multiple tasks at the same time. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.