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 springBoot @ Scheduled enables multiple tasks to start execution at the same time

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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.

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

Development

Wechat

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

12
Report