定时任务
@Component public class TestTask { //@Scheduled(fixedRate = 2000)//两秒执行一次(从上一次开始计算2秒后) //@Scheduled(fixedDelay = 2000)//每次执行间隔两秒 //@Scheduled(cron="*/2 * * * * *")//每次执行间隔两秒 @Scheduled(fixedDelayString = "2000")//每次执行间隔两秒,这个参数可以同配置文件配置 public void sum() throws Exception { Thread.sleep(4000); System.out.println("当前时间:"+new Date()); } }
异步任务
@Component @Async public class AsyncTask { public void task1() throws InterruptedException { Long start = System.currentTimeMillis(); Thread.sleep(1000); Long end = System.currentTimeMillis(); System.out.println("任务1耗时:"+(end-start)); } public void task2() throws InterruptedException { Long start = System.currentTimeMillis(); Thread.sleep(2000); Long end = System.currentTimeMillis(); System.out.println("任务2耗时:"+(end-start)); } public void task3() throws InterruptedException { Long start = System.currentTimeMillis(); Thread.sleep(3000); Long end = System.currentTimeMillis(); System.out.println("任务3耗时:" + (end - start)); } public Future<String> task4() throws InterruptedException { Long start = System.currentTimeMillis(); Thread.sleep(1000); Long end = System.currentTimeMillis(); System.out.println("任务4耗时:"+(end-start)); return new AsyncResult<String>("任务4结束"); } public Future<String> task5() throws InterruptedException { Long start = System.currentTimeMillis(); Thread.sleep(2000); Long end = System.currentTimeMillis(); System.out.println("任务5耗时:"+(end-start)); return new AsyncResult<String>("任务5结束"); } public Future<String> task6() throws InterruptedException { Long start = System.currentTimeMillis(); Thread.sleep(3000); Long end = System.currentTimeMillis(); System.out.println("任务6耗时:"+(end-start)); return new AsyncResult<String>("任务6结束"); } }
调用异步任务:
@RestController @RequestMapping("/api/v1/task") public class MyTaskController { @Autowired AsyncTask aTask; @RequestMapping("/async") public void doTask() throws InterruptedException { long start = System.currentTimeMillis(); aTask.task1(); aTask.task2(); aTask.task3(); Future<String> t4 = aTask.task4(); Future<String> t5 = aTask.task5(); Future<String> t6 = aTask.task6(); for(;;) { // if(aTask.task4().isDone()&&aTask.task5().isDone()&&aTask.task6().isDone()) if(t4.isDone()&&t5.isDone()&&t6.isDone()) { break; } } Thread.sleep(1000); long end = System.currentTimeMillis(); System.out.print("请求耗时:"+(end-start)); } }
相关推荐
【java框架】SpringBoot(4)--SpringBoot实现异步、邮件、定时任务(csdn)————程序
1. SpringBoot 自定义线程池以及多线程间的异步调用(@Async、@EnableAsync) 2.Java多线程之定时任务 以及 SpringBoot多线程实现定时任务 3.@EnableScheduling 与 @Scheduled
本视频《SpringBoot高级》属于下部,着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),...
Elasticsearch快速入门 22、尚硅谷-SpringBoot高级-检索-SpringBoot整合Jest操作ES 23、尚硅谷-SpringBoot高级-检索-整合SpringDataElasticsearch 24、尚硅谷-SpringBoot高级-任务-异步任务 25、尚硅谷-SpringBoot...
springboot-websocket整合项目开箱即用,ws服务端,包含了在线客户监听,发送指定客户端数据,发送所有客户端数据。还可以根据指定转发给某个客户端。 WebSocketConfig配置缓存大小设置,设置异步发送操作的超时时间...
springboot 定时任务(线程配置,并行【同步】、异步等)
主要为大家详细介绍了SpringBoot实现定时任务和异步调用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
01-SpringBoot介绍 ...18-SpringBoot定时任务 19-SpringBoot实现异步 20-SpringBoot自定义参数 21-SpringBoot启动端口访问路径 22-yml配置文件+端口占用解决方法 23-SpringBoot多环境区分 24-SpringBoot打包发布
01-SpringBoot介绍 ...18-SpringBoot定时任务 19-SpringBoot实现异步 20-SpringBoot自定义参数 21-SpringBoot启动端口访问路径 22-yml配置文件+端口占用解决方法 23-SpringBoot多环境区分 24-SpringBoot打包发布
最新的springboot整合rocketmq消息发送和消费的功能,主要测试通过的案例如下: 功能特性: - [x] 同步发送 - [x] 同步顺序发送 - [ ] 同步批量发送 - [x] 异步发送 - [x] 异步顺序发送 - [x] 顺序消费 - [ ...
Ajax-springboot-cms.zip,基于springboot、spring、springmvc、mybatis、freemarker、bootstrap、apache shiro、quartz、email、mysql等的内容管理系统。,ajax代表异步javascript和xml。它是多种web技术的集合,包括...
疫情数据定时同步更新于丁香、腾讯和天行数据API,项目前台融合了Echarts,AmazeUI,BootStrap等框架;数据采集方面采用HttpClient发送网络请求并使用jsoup解析HTML数据和对接天行数据API,数据处理方面采用fastjson、...
SpringBoot视频教程 ...18-SpringBoot定时任务 19-SpringBoot实现异步 20-SpringBoot自定义参数 21-SpringBoot启动端口访问路径 22-yml配置文件+端口占用解决方法 23-SpringBoot多环境区分 24-SpringBoot打包发布
SpringBoot接口 - 如何生成接口文档之集成Smart-Doc上文我们看到可以通过Swagger系列可以快速生成API文档, 但是这种API文档生成是需要在接口上添加注解等,这表明这是一种侵入式方式; 那么有没有非侵入式方式呢, ...
SpringBoot各模块整合-入门项目介绍 包含:atomikos,shiro,mybatis,mybatis-plus,jpa,jdbc-template,redis,mongodb,docker,jpa,admin,异步,apidoc,thymeleaf,文件上传,邮件等等 (1)快速建造 git ...
springboot-ssmspringboot整合mybatis(SSM项目整合)springboot整合SSM项目,分页插件...也整合了task以及异步任务(线程池中执行的任务)。也是自己的移动端的app的后台,主要为移动端提供token的更新以及验证等操作。
还有就是测试了异步和定时任务的功能 multipledatasources2: 通过分包实现多数据源的,检测多数据源存在的事务管理问题 multipledatasources3: 通过jta-atomikos解决传统项目多数据源事务管理问题 ...
Spring Boot 2.0 基于 Spring 5 Framework ,提供了异步非阻塞 IO 的响应式 Stream 、非堵塞的函数式 Reactive Web 框架 Spring WebFlux。
springboot异步任务案例,通过注解来实现定时器,以及相应的异步任务的执行。
引入mq的目的应用解耦、流量削峰、异步任务,提高系统的吞吐量cpu 密集型的线程的数量为 cpu 核心数+1io 密集型的线程数为 2*N线程中断如果线程中没有