SpringBoot几种定时任务的实现方式 和多线程执行任务

定时任务实现的几种方式:

  • Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少。
  • ScheduledExecutorService:也jdk自带的一个类;是基于线程池设计的定时任务类,每个调度任务都会分配到线程池中的一个线程去执行,也就是说,任务是并发执行,互不影响。
  • Spring Task:Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多。
  • Quartz:这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂。

下面说的是 spring自带的定时任务。

SpringBoot使用注解方式开启定时任务添加注解方式

开发中定时任务,要和别的业务类分开写。这样处理起来方便。

1)启动类里面 @EnableScheduling开启定时任务,自动扫描
2)定时任务业务类 加注解 @Component被容器扫描
3)定时执行的方法加上注解 @Scheduled(fixedRate=2000) 定期执行一次


@SpringBootApplication //一个注解顶下面3个
@EnableScheduling //开启定时任务
@EnableAsync //开启异步任务
public class XdclassApplication {

public static void main(String[] args) {
SpringApplication.run(XdclassApplication.class, args);
}
}

//定时任务类
@Component
public class AsyncTask {
@Scheduled(fixedRate=2000)
public void task1() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(1000L);
long end = System.currentTimeMillis();
System.out.println("任务1耗时="+(end-begin));
}
public void task2() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(2000L);
long end = System.currentTimeMillis();
System.out.println("任务2耗时="+(end-begin));
}
//定时任务需要返回值的情况
public Future<String> task4() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(2000L);
long end = System.currentTimeMillis();
System.out.println("任务4耗时="+(end-begin));
return new AsyncResult<String>("任务4");
}
public Future<String> task5() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(3000L);
long end = System.currentTimeMillis();
System.out.println("任务5耗时="+(end-begin));
return new AsyncResult<String>("任务5");
}

SpringBoot2.x异步任务实战(核心知识) 不一定是要和上面的定时任务一起使用还有很多场景的。需要异步处理的任务,最好单独的封装在一个类中。这样好处理。
简介:讲解什么是异步任务,和使用SpringBoot2.x开发异步任务实战
1、什么是异步任务和使用场景:适用于处理log、发送邮件、短信……等
下单接口->查库存 100
余额校验 150
风控用户100

2、启动类里面使用@EnableAsync注解开启功能,自动扫描

3、定义异步任务类并使用@Component标记组件被容器扫描,异步方法加上@Async
注意点:
1)要把异步任务封装到类里面,不能直接写到Controller
2)增加Future<String> 返回结果 AsyncResult<String>("task执行完成");
3)如果需要拿到结果 需要判断全部的 task.isDone()
4、通过注入方式,注入到controller里面,如果测试前后区别则改为同步则把Async注释掉

@Component
@Async
public class AsyncTask {
@Scheduled(fixedRate=2000)
public void task1() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(1000L);
long end = System.currentTimeMillis();
System.out.println("任务1耗时="+(end-begin));
}
public void task2() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(2000L);
long end = System.currentTimeMillis();
System.out.println("任务2耗时="+(end-begin));
}
//定时任务需要返回值的情况
public Future<String> task4() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(2000L);
long end = System.currentTimeMillis();
System.out.println("任务4耗时="+(end-begin));
return new AsyncResult<String>("任务4");
}
public Future<String> task5() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(3000L);
long end = System.currentTimeMillis();
System.out.println("任务5耗时="+(end-begin));
return new AsyncResult<String>("任务5");
}
}

@GetMapping("async_task")
public JsonData exeTask() throws InterruptedException{

long begin = System.currentTimeMillis();

Future<String> task4 = task.task4();
Future<String> task5 = task.task5();
for(;;){
if (task4.isDone() && task5.isDone() ) {
break;
}
}

long end = System.currentTimeMillis();

long total = end-begin;
System.out.println("执行总耗时="+total);
return JsonData.buildSuccess(total);
}

猜你喜欢

转载自www.cnblogs.com/xiaowangbangzhu/p/10321238.html