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

What is the multithreaded concurrent timing task of spring boot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Spring boot multi-threaded concurrent timing task is what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Spring-boot | Multithreaded concurrent timing tasks

I just read the article about the implementation of timing tasks in Spring Boot, and I feel good. It is very simple and convenient for Spring Boot to use the Schedule that comes with Spring to implement scheduled tasks. I'd like to share with you here.

Open cache annotation @ SpringBootApplication@EnableScheduling / / enable timed task public class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args);}} write scheduled task [@ Component] (https://my.oschina.net/u/3907912)public class ScheduledTasks {private Logger logger = LoggerFactory.getLogger (ScheduledTasks.class)) / / cron accepts the cron expression and determines the timing rule @ Scheduled according to the cron expression. / / public void testCron () {DateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") is executed every 5 seconds; logger.info (sdf.format (new Date ()) + "* every 5 seconds") }} Task completed

Start the project, view the console print information, and find that the scheduled task has taken effect. The integration of spring boot and Scheduled is complete.

There are problems

But then a problem was discovered, and by testing several tasks at the same time, it was found that all the tasks were done by the same thread in the same thread pool. In the actual development process, we certainly don't want all tasks to run in one thread.

@ Scheduled (cron= "0amp 1 *?") / / execute public void testCron1 () {DateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") every second; logger.info (sdf.format (new Date ()) + "* every second") } @ Scheduled (cron= "0amp 2 *?") / / execute public void testCron2 () {DateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") every 2 seconds; logger.info (sdf.format (new Date ()) + "* every 2 seconds") } @ Scheduled (cron= "0amp 3 *?") / / execute public void testCron3 () {DateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") every 3 seconds; logger.info (sdf.format (new Date ()) + "* every 3 seconds") } @ Scheduled (cron= "0amp 4 *?") / / execute public void testCron4 () {DateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") every 4 seconds; logger.info (sdf.format (new Date ()) + "* every 4 seconds");}

Solution

So, how is it designed to be multithreaded for concurrency? I have seen such a solution on the Internet. We tried to configure it by implementing the SchedulingConfigurer interface through the ScheduleConfig configuration file and overriding the setSchedulerfang method.

@ Configurationpublic class ScheduleConfig implements SchedulingConfigurer {@ Override public void configureTasks (ScheduledTaskRegistrar taskRegistrar) {taskRegistrar.setScheduler (Executors.newScheduledThreadPool (5));}} Integration succeeded

This completes the configuration of multithreading concurrency? We start the project to verify the results through the console output information, and finally find that all the tasks are completed in the same thread pool but in different threads, which shows that this scheme is completely feasible. In this way, we have completed the spring boot multithreading and timing task.

Note

Parameters supported by @ Scheduled:

1.cron:cron expression, which specifies that the task is executed at a specific time; 2.fixedDelay: indicates how long it will be executed again after the last task is executed. The parameter type is long, unit ms; 3.fixedDelayString: same as fixedDelay, except that the parameter type is changed to String; 4.fixedRate: indicates that the task is executed at a certain frequency, and the parameter type is long, unit ms; 5.fixedRateString: the same meaning as fixedRate, but the parameter type is changed to String 6.initialDelay: indicates how long it takes to delay the execution of the task for the first time. The parameter type is long, and the unit ms; 7.initialDelayString: is the same as initialDelay, except that the parameter type is changed to String; 8.zone: time zone. The default is the current time zone, which is generally not used.

Example of Cron expressions:

Execute every 5 seconds: * / 5 *? Execute every 1 minute: 0 * / 1 *? Execute it at 23:00 every day: 0.023 *? Execute every day at 1: 00 a. M.: 0 01 *? Execute at 1: 00 a. M. on the 1st of each month: 0 011 *? Executed at 23:00 on the last day of each month: 0.023 L *? It is implemented every Sunday at 1: 00 a. M.: 0 01? * L at 26: 00, 29: 00, 33: 00: 026, 29, 33 *? Every day at 0 o'clock, 13:00, 18:00 and 21:00, it is executed: 0, 000, 13, 18, and 21 * *?

In fact, do not Cron expressions and do not worry, there are many online Cron generators, we can generate cron that meets the requirements through the online generator, which is also very convenient.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report