高端网站制作价格,ui培训机构排名,网站制作公司多少钱,wordpress弹出相册1、功能概述
任务调度就是在规定的时间内执行的任务或者按照固定的频率执行的任务。是非常常见的功能之一。常见的有JDK原生的Timer, ScheduledThreadPoolExecutor以及springboot提供的Schduled。分布式调度框架如QuartZ、Elasticjob、XXL-JOB、SchedulerX、PowerJob等。
本文…1、功能概述
任务调度就是在规定的时间内执行的任务或者按照固定的频率执行的任务。是非常常见的功能之一。常见的有JDK原生的Timer, ScheduledThreadPoolExecutor以及springboot提供的Schduled。分布式调度框架如QuartZ、Elasticjob、XXL-JOB、SchedulerX、PowerJob等。
本文主要讲解非分布式环境下的Scheduled任务调度讲解以及Scheduled结合多线程和Async异步任务的使用。
当然在任务不是很多的情况下Scheduled也可以结合如Redis的锁机制实现分布式的任务调度但是还是建议在分布式环境下使用分布式调度框架如QuartZ、Elasticjob、XXL-JOB、SchedulerX、PowerJob等。
2、Scheduled基本使用
2.1、创建springboot工程引入包信息
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.1.6/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.txc/groupIdartifactIdscheduleddemo/artifactIdversion0.0.1-SNAPSHOT/versionnamescheduleddemo/namedescriptionscheduleddemo/descriptionpropertiesjava.version17/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationimagebuilderpaketobuildpacks/builder-jammy-base:latest/builder/imageexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project2.2、按照固定间隔执行
fixedDelay按照固定间隔执行上一个任务的结束到下一个任务的开始间隔。
initialDealay:延迟启动启动之后指定时间再执行调度任务
EnableScheduling开启任务调度写在类上只开启当前类中的任务调度如果写在启动类上则开启项目中的所有任务调度。
Slf4j
//加载类型开启类中加载启动类上开启整个项目
EnableScheduling //是否开启
Component
public class MyScheduled {Scheduled(fixedDelay 3000,initialDelay 3000)public void process(){log.info(process执行 LocalDateTime.now());}
}结果分析
从输出结果中可以看出程序每隔3s执行一次 2.3、按照固定频率执行任务
说明1fixedRate:按照固定频率执行任务如每三秒执行一次上一个任务下次任务的开始由于此时是单线程下一个任务开始需要等上一个任务结束。
说明2我们通过Thread.sleep(5000)设置任务执行需要2s时间
Slf4j
//加载类型开启类中加载启动类上开启整个项目
EnableScheduling //是否开启
Component
public class MyScheduled {
Scheduled(fixedRate 3000,initialDelay 3000)
public void process() throws InterruptedException {
log.info(process执行fixedRate开始 LocalDateTime.now());Thread.sleep(2000);log.info(process执行fixedRate结束 LocalDateTime.now());}
}结果分析从结果中可以看出由于设置process执行的时间为2s钟process按照固定的频率(3s)每3s执行一次第一次开始是22:19:22第二次开始是22:19:25 2.4、按照固定频率执行任务
说明1fixedRate:按照固定频率执行任务如每三秒执行一次上一个任务下次任务的开始由于此时是单线程下一个任务开始需要等上一个任务结束。
说明2我们通过Thread.sleep(5000)设置任务执行需要5s时间
Slf4j
//加载类型开启类中加载启动类上开启整个项目
EnableScheduling //是否开启
Component
public class MyScheduled {
Scheduled(fixedRate 3000,initialDelay 3000)
public void process() throws InterruptedException {log.info(process执行fixedRate开始 LocalDateTime.now());Thread.sleep(5000);log.info(process执行fixedRate结束 LocalDateTime.now());}
}结果分析
从结果可以看出虽然设置固定的频率是3s但是由于在单线程情况下下次任务的开启需要等待上一个任务的结束第一次任务开始时间为22:17:43第二次任务开启时间为22:17:48中间间隔了5s钟。 2.5、通过公式设置定时任务
cron:可以通过特性的公式设定定时任务任务生成网站https://cron.qqe2.com/
如可以设置每周三下午五点执行每月的月尾执行一次等。 如上图生成的语法表示每分钟的前五秒执行process
Slf4j
//加载类型开启类中加载启动类上开启整个项目
EnableScheduling //是否开启
Component
public class MyScheduled {
Scheduled(cron 0,1,2,3,4 * * * * ? )
public void process() throws InterruptedException {log.info(process执行fixedRate开始 LocalDateTime.now());Thread.sleep(5000);log.info(process执行fixedRate结束 LocalDateTime.now());}
}结果分析
从图中可以看出每每分钟开始的时候执行五秒后结束。 3、Scheduled与多线程
加入多线程的目的是为了程序执行的效率能够提高。但是在设置多线程的时候不能开辟过多的线程因为线程资源非常的消耗cpu资源必要的时候需要使用分布式任务调度。
3.1、非多线程的情况
理论上当process1结束的时候下次process1启动的时候需要等待process2执行结束否则1不能启动应该这个时候依旧是单线程。
Slf4j
//加载类型开启类中加载启动类上开启整个项目
EnableScheduling //是否开启
Component
public class MyScheduled {
Scheduled(fixedDelay 3000)
public void process1() throws InterruptedException {
log.info(process1执行开始 LocalDateTime.now());Thread.sleep(5000);log.info(process1执行结束 LocalDateTime.now());}
Scheduled(fixedDelay 3000)
public void process2() throws InterruptedException {log.info(process2执行开始 LocalDateTime.now());Thread.sleep(5000);log.info(process2执行结束 LocalDateTime.now());}
}结果分析
从输出结果可以看出process2的开始是等到process1结束后才执行的。 3.2、多线程的情况
在启动类中定义线程池。值不需要设置太大现成对cpu资源消耗大搞不好容易让系统宕机。
设置多线程后直接启动程序继续观看process1和process2的输出情况。
package com.txc.scheduleddemo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;SpringBootApplication
public class ScheduleddemoApplication {public static void main(String[] args) {SpringApplication.run(ScheduleddemoApplication.class, args);}Beanpublic TaskScheduler taskScheduler(){ThreadPoolTaskScheduler taskSchedulernew ThreadPoolTaskScheduler();//设置线程池中线程的数量//多线程对cpu资源消耗较大值不能太大。taskScheduler.setPoolSize(5);return taskScheduler;}}结果分析
process1和process2使用的是不同的线程,一个线程为taskSheduler-1,一个线程为taskSheduler-2。
而且process1和process2是同时启动的没有出现相互等待的情况因为现在使用的是多线程的情况。 4、Scheduled与 Async异步任务
在上面的案例中虽然process1和process2同时执行了没有出现相互等待的情况。但是第二次process1和process2执行依旧是等待程序5s结束后再等待3是执行。
name如何能够实现即使process1执行时间为5s,但是下一次process1的启动依旧是3s后。而不是当前的8是后。这就可以使用异步任务Async。当然复杂的异步任务还是建议使用如MQ技术。
注意点Async的使用需要写在单独的一个类中不能与当前调用业务写在一起否则不生效。
完全不会使用Async看如下博客
https://blog.csdn.net/tangshiyilang/article/details/129440283
4.1、创建异步任务类及异步方法
Component
public class AsyncTaskScheduled {Async//那个方法需要使用异步调用就使用该注解public void asyncMethod() {try{Thread.sleep(6000);//模拟异步执行业务的时间}catch (Exception e){System.out.println(e.getStackTrace());}}
}4.2、需要再启动类上开启异步任务
EnableAsync开启异步任务调度
SpringBootApplication
EnableAsync
public class ScheduleddemoApplication {public static void main(String[] args) {SpringApplication.run(ScheduleddemoApplication.class, args);}Beanpublic TaskScheduler taskScheduler(){ThreadPoolTaskScheduler taskSchedulernew ThreadPoolTaskScheduler();//设置线程池中线程的数量//多线程对cpu资源消耗较大值不能太大。taskScheduler.setPoolSize(10);return taskScheduler;}}4.3、创建process3和process4方法
process3和process3与之前的process1和process2方法一样都是基于多线程操作。
Slf4j
//加载类型开启类中加载启动类上开启整个项目
EnableScheduling //是否开启
Component
public class MyScheduled {
Autowired
AsyncTaskScheduled asyncTaskScheduled;
Scheduled(fixedDelay 3000)
public void process3() throws InterruptedException {
log.info(process3执行开始 LocalDateTime.now());asyncTaskScheduled.asyncMethod();log.info(process3执行结束 LocalDateTime.now());}
Scheduled(fixedDelay 3000)
public void process4() throws InterruptedException {log.info(process4执行开始 LocalDateTime.now());asyncTaskScheduled.asyncMethod();log.info(process4执行结束 LocalDateTime.now());}
}结果分析
从结果可以看出虽然异步任务执行的时间为6s,但是process4第一次开始和第二次开始的时间间隔为3s. 5、源码下载
https://download.csdn.net/download/tangshiyilang/88627612