In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand Java spring timed tasks". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to understand Java spring timed tasks.
Catalogue
I. scheduled tasks
1. Cron expression
2. Cron example
3. SpringBoot integration
Summary
1. Timed task 1. Cron expression
Grammar: seconds, minutes, days, months, weeks and years
("year" Spring is not supported, which means that it can only be set in spring scheduled tasks: seconds, minutes, day, month and week)
2. Cron example
3. SpringBoot integration
@ EnableScheduling
@ Scheduled
Example:
Package com.xunqi.gulimall.seckill.scheduled;import lombok.extern.slf4j.Slf4j;import org.springframework.scheduling.annotation.Async;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;/** * @ Description: * @ Created: with IntelliJ IDEA. * @ author: Xia Mo Zhishui * @ createTime: 2020-07-09 18:49 * * / * timed task * 1, @ EnableScheduling starts timed task * 2, @ Scheduled starts a scheduled task * * asynchronous task * 1, @ EnableAsync: starts asynchronous task * 2, @ Async: label the method that you want to execute asynchronously with * / @ Slf4j@Component@EnableSchedulingpublic class HelloScheduled {/ * 1. In Spring, the expression is made up of 6 bits. The seventh year * 2, the position on what day of the week, 1-7 represents Monday to Sunday * 3, scheduled tasks should not be blocked. The default is blocked * 1), the business can be submitted to the thread pool * CompletableFuture.runAsync (()-> {*}, execute) asynchronously; * * 2), and the thread pool for scheduled tasks is supported. Set TaskSchedulingProperties * spring.task.scheduling.pool.size: 5 * * 3), let scheduled tasks execute * asynchronous tasks asynchronously * * solution: use asynchronous tasks + scheduled tasks to complete the function that scheduled tasks do not block * * / @ Scheduled (cron = "* / 1 *?") Public void hello () {log.info ("hello..."); try {TimeUnit.SECONDS.sleep (3);} catch (InterruptedException e) {e.printStackTrace ();}
Scheduled tasks are blocked threads by default, that is, even if you are set to execute once every second, but the business time within the method takes 5 seconds to complete, it will cause scheduled tasks to be executed every 6 seconds.
Of course, we can start asynchronous threads:
@ EnableAsync
@ Async
Example:
Package com.xunqi.gulimall.seckill.scheduled;import lombok.extern.slf4j.Slf4j;import org.springframework.scheduling.annotation.Async;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;/** * @ Description: * @ Created: with IntelliJ IDEA. * @ author: Xia Mo Zhishui * @ createTime: 2020-07-09 18:49 * * / * timed task * 1, @ EnableScheduling starts timed task * 2, @ Scheduled starts a scheduled task * * asynchronous task * 1, @ EnableAsync: starts asynchronous task * 2, @ Async: label the method that you want to execute asynchronously with * / @ Slf4j@Component@EnableAsync@EnableSchedulingpublic class HelloScheduled {/ * 1. In Spring, the expression is made up of 6 bits. The seventh year * 2, the position on what day of the week, 1-7 represents Monday to Sunday * 3, scheduled tasks should not be blocked. The default is blocked * 1), the business can be submitted to the thread pool * CompletableFuture.runAsync (()-> {*}, execute) asynchronously; * * 2), and the thread pool for scheduled tasks is supported. Set TaskSchedulingProperties * spring.task.scheduling.pool.size: 5 * * 3), let scheduled tasks execute * asynchronous tasks asynchronously * * solve: use asynchronous tasks + scheduled tasks to complete the function that scheduled tasks do not block * * / @ Async @ Scheduled (cron = "* / 1 *?") Public void hello () {log.info ("hello..."); try {TimeUnit.SECONDS.sleep (3);} catch (InterruptedException e) {e.printStackTrace ();}
This opens an asynchronous thread, and it is a non-blocking thread, because one thread is opened to execute each time. We can take a look at the screenshot of the source code configuration. This is the default configuration of asynchronous execution. The number of core threads is 8. The maximum number of threads is infinite. If it is executed once a second, the server will be exhausted.
Of course, we can set the thread pool for scheduled tasks in the configuration file:
# number of core threads
Spring.task.execution.pool.core-size=20
# maximum number of threads
Spring.task.execution.pool.max-size=50
# queue size
Spring.task.execution.pool.queue-capacity=10000
Thank you for your reading, the above is the content of "how to understand Java spring scheduled tasks". After the study of this article, I believe you have a deeper understanding of how to understand Java spring scheduled tasks, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.