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

昆明网站制作游戏钓鱼网站怎么做

昆明网站制作,游戏钓鱼网站怎么做,做网站用html5,德州核酸检测最新公告一、使用Async实现异步调用 在Spring Boot中#xff0c;我们只需要通过使用Async注解就能简单的将原来的同步函数变为异步函数#xff0c;Task类实现如下#xff1a; 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 CompletableFutureString 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 CompletableFutureString 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 CompletableFutureString 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 CompletableFutureString 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 CompletableFutureString 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 CompletableFutureString 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();CompletableFutureString task1 asyncTasks.doTaskOne();CompletableFutureString task2 asyncTasks.doTaskTwo();CompletableFutureString 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.eeditor.cn/news/121724/

相关文章:

  • 水产网站模板wordpress 企业模板 免费
  • 阿里邮箱企业版官网网站做优化需要哪些后台信息
  • 江苏公司网站建设效果图网站有哪些
  • 京东商城官方网站品牌营销推广代运营
  • 江西做网站找谁百度seo 站长工具
  • 网站js聊天代码烟台主流网站
  • 石家庄网站建设平台wordpress添加单页模板
  • 网站版块模板免费申请空间的地址有哪些
  • 网站建设云南wordpress移动端广告添加
  • 怎么搭建源码网站网站备案 时间
  • 精品课程网站建设 碧辉腾乐常州住房和城乡建设部网站
  • 网站科普信息化建设的意义红酒网页设计素材
  • 做网站用什么软件啊成都网站制作哪家专业
  • 河南省建设工程招投标协会网站贸易公司 网站 扶持
  • 如何控制一个网站软件开发射阳做网站
  • 常用网站开发模式wordpress怎么新建栏目
  • 房产网有哪些网站虚拟主机服务
  • wordpress 柚子皮下载单页关键词优化费用
  • 邢台12345网站视频网站开发流程
  • php彩票网站建设源码网站建设哪家最好
  • 网站制作怎样做wordpress 安装 畅言
  • 电子商务网站建设课程性质公司网站建设排名
  • 论坛做视频网站有哪些小软件制作教程
  • 百度做网站怎么联系wordpress主题播放音乐不刷新
  • 扬州门户网站开发济南中京网站建设公司
  • 网站模板中心中小企业网贷平台
  • wordpress建站图片效果有哪些做的好看的网站吗
  • 深圳市专业做网站太原中企动力网站建设
  • 长沙城乡建设网站首页国外做兼职的网站
  • 站中站网站案例wordpress弹窗表单