当前位置: 首页 > news >正文

烟台定制网站建设报价sem搜索

烟台定制网站建设报价,sem搜索,如何制作个人网站,郑州做网站优化公司一、使用Async实现异步调用 在Spring Boot中,我们只需要通过使用Async注解就能简单的将原来的同步函数变为异步函数,Task类实现如下: package com.example.demospringboot;import lombok.extern.slf4j.Slf4j; import org.springframework.s…

一、使用@Async实现异步调用

在Spring Boot中,我们只需要通过使用@Async注解就能简单的将原来的同步函数变为异步函数,Task类实现如下:

package com.example.demospringboot;import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;import java.util.Random;
import java.util.concurrent.CompletableFuture;@Slf4j
@Component
public class AsyncTasks {public static Random random = new Random();@Asyncpublic CompletableFuture<String> doTaskOne() throws Exception {log.info("开始做任务一");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();log.info("完成任务一,耗时:" + (end - start) + "毫秒");return CompletableFuture.completedFuture("任务一完成");}@Asyncpublic CompletableFuture<String> doTaskTwo() throws Exception {log.info("开始做任务二");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();log.info("完成任务二,耗时:" + (end - start) + "毫秒");return CompletableFuture.completedFuture("任务二完成");}@Asyncpublic CompletableFuture<String>  doTaskThree() throws Exception {log.info("开始做任务三");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();log.info("完成任务三,耗时:" + (end - start) + "毫秒");return CompletableFuture.completedFuture("任务三完成");}
}

注:@Async所修饰的函数不要定义为static类型,这样异步调用不会生效

为了让@Async注解能够生效,还需要在Spring Boot的主程序中配置@EnableAsync,如下所示:

package com.example.demospringboot;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;@EnableAsync
@SpringBootApplication
public class DemospringbootApplication {public static void main(String[] args) {SpringApplication.run(DemospringbootApplication.class, args);}}

测试类如下:

package com.example.demospringboot;import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.cache.CacheManager;@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemospringbootApplicationTests {@Autowiredprivate AsyncTasks asyncTasks;@Testpublic void test() throws Exception {asyncTasks.doTaskOne();asyncTasks.doTaskTwo();asyncTasks.doTaskThree();}}

此时可以反复执行单元测试,您可能会遇到各种不同的结果,比如:

2023-08-01 21:32:46.064  INFO 1764 --- [         task-1] com.example.demospringboot.AsyncTasks    : 开始做任务一
2023-08-01 21:32:46.064  INFO 1764 --- [         task-3] com.example.demospringboot.AsyncTasks    : 开始做任务三
2023-08-01 21:32:46.064  INFO 1764 --- [         task-2] com.example.demospringboot.AsyncTasks    : 开始做任务二

异步回调

那么我们如何判断上述三个异步调用是否已经执行完成呢?我们需要使用CompletableFuture来返回异步调用的结果,就像如下方式改造doTaskOne函数:

package com.example.demospringboot;import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;import java.util.Random;
import java.util.concurrent.CompletableFuture;@Slf4j
@Component
public class AsyncTasks {public static Random random = new Random();@Asyncpublic CompletableFuture<String> doTaskOne() throws Exception {log.info("开始做任务一");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();log.info("完成任务一,耗时:" + (end - start) + "毫秒");return CompletableFuture.completedFuture("任务一完成");}@Asyncpublic CompletableFuture<String> doTaskTwo() throws Exception {log.info("开始做任务二");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();log.info("完成任务二,耗时:" + (end - start) + "毫秒");return CompletableFuture.completedFuture("任务二完成");}@Asyncpublic CompletableFuture<String>  doTaskThree() throws Exception {log.info("开始做任务三");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();log.info("完成任务三,耗时:" + (end - start) + "毫秒");return CompletableFuture.completedFuture("任务三完成");}}

下面我们改造一下测试用例,让测试在等待完成三个异步调用之后来做一些其他事情

package com.example.demospringboot;import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.concurrent.CompletableFuture;@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemospringbootApplicationTests {@Autowiredprivate AsyncTasks asyncTasks;@Testpublic void test() throws Exception {long start = System.currentTimeMillis();CompletableFuture<String> task1 = asyncTasks.doTaskOne();CompletableFuture<String> task2 = asyncTasks.doTaskTwo();CompletableFuture<String> task3 = asyncTasks.doTaskThree();CompletableFuture.allOf(task1, task2, task3).join();long end = System.currentTimeMillis();log.info("任务全部完成,总耗时:" + (end - start) + "毫秒");}}

执行一下上述的单元测试,可以看到如下结果:

2023-08-01 21:41:40.347  INFO 13684 --- [         task-1] com.example.demospringboot.AsyncTasks    : 开始做任务一
2023-08-01 21:41:40.347  INFO 13684 --- [         task-3] com.example.demospringboot.AsyncTasks    : 开始做任务三
2023-08-01 21:41:40.347  INFO 13684 --- [         task-2] com.example.demospringboot.AsyncTasks    : 开始做任务二
2023-08-01 21:41:44.817  INFO 13684 --- [         task-2] com.example.demospringboot.AsyncTasks    : 完成任务二,耗时:4470毫秒
2023-08-01 21:41:45.042  INFO 13684 --- [         task-1] com.example.demospringboot.AsyncTasks    : 完成任务一,耗时:4695毫秒
2023-08-01 21:41:48.154  INFO 13684 --- [         task-3] com.example.demospringboot.AsyncTasks    : 完成任务三,耗时:7807毫秒
2023-08-01 21:41:48.154  INFO 13684 --- [           main] c.e.d.DemospringbootApplicationTests     : 任务全部完成,总耗时:7817毫秒

可以看到,通过异步调用,让任务一、二、三并发执行,有效的减少了程序的总运行时间。

参考:https://blog.didispace.com/spring-boot-learning-2-7-5/

http://www.rdtb.cn/news/19580.html

相关文章:

  • 小程序微信怎么开发抖音seo代理
  • 电商网站开发费用登录百度账号
  • 内存128mb做网站够吗上海seo外包公司
  • 重庆潼南网站建设鞍山seo外包
  • 批发网站宁德市医院
  • 厦门专业网站制作自己做网站需要什么条件
  • 阿里云网站备案信息真实性核验单seo研究中心培训机构
  • 做中英文网站 java快速关键词排名首页
  • 搭建什么网站比较赚钱制作网站教学
  • 厦门微网站建设公司广东省自然资源厅
  • 怎么做类似淘宝网站如何优化网络连接
  • 网站开发的英文书有什么海城seo网站排名优化推广
  • 官网网站建设收费seo外链友情链接
  • 龙岗区做网站教育培训网
  • 长春网站设计团队百度关键词自然排名优化公司
  • 如何在各个购物网站之间做差价九江seo优化
  • wordpress 安装权限武汉seo网站排名优化公司
  • 如何创建个人的网站秦皇岛seo优化
  • 佛山网站建设公司排名seo关键词优化系统
  • 备案期间网站可以做竞价吗企业网站的在线推广方法有
  • 东莞做网站定制十大推广app平台
  • 做网站网页的人是不是思维爱站网关键词密度
  • 福州做网站需要多少钱百度一下你就知道网页
  • 中国人做网站卖美国人做个网页价格多少
  • 濮阳免费网站建设百度拍照搜索
  • 建设电影网站如何赚钱深圳关键词优化公司哪家好
  • 新网网站内部优化互联网广告管理暂行办法
  • 周宁县建设局网站免费com域名注册网站
  • 营销网站开发找哪家百度搜索榜单
  • 做b2b网站赚钱吗襄阳seo培训