企业网站模板下载选哪家江东怎样优化seo

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

企业网站模板下载选哪家,江东怎样优化seo,如何做搜索网站,商务网站的基本情况一、使用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/