东莞网站设计讯息网站内地图位置怎么做
- 作者: 五速梦信息网
- 时间: 2026年03月21日 11:20
当前位置: 首页 > news >正文
东莞网站设计讯息,网站内地图位置怎么做,24小时自动发货网站建设,wordpress支付下载地址Git仓库
https://gitee.com/Lin_DH/system
介绍
同步调用#xff1a;指程序在执行时#xff0c;调用方需要等待函数调用返回结果后#xff0c;才能继续执行下一步操作#xff0c;是一种阻塞式调用。 异步调用#xff1a;指程序在执行时#xff0c;调用方在调用函数后立…Git仓库
https://gitee.com/Lin_DH/system
介绍
同步调用指程序在执行时调用方需要等待函数调用返回结果后才能继续执行下一步操作是一种阻塞式调用。 异步调用指程序在执行时调用方在调用函数后立即返回不需要等待函数调用返回结果可以直接执行下一步操作当函数执行完成后会通过回调或其他方式通知调用方得到返回结果。 回调在调用一个函数后需要在函数执行中或执行后将执行结果或状态返回给调用者。
代码实现
第一步启动类上添加 EnableAsync 注解开启异步功能。
EnableAsync
SpringBootApplication
public class SystemApplication extends SpringBootServletInitializer {}同步调用
第二步添加同步调用业务逻辑 注Async 注解不能修饰的 static 修饰的函数该类型的函数异步调用不会生效。
package com.lm.system.task;import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import java.util.Random;/*** 同步调用* author DUHAOLIN* date 2024/10/17/
Slf4j
Component
public class SyncTask {public static Random random new Random();public void one() throws InterruptedException {commonTask(一);}public void two() throws InterruptedException {commonTask(二);}public void three() throws InterruptedException {commonTask(三);}public void commonTask(String s) throws InterruptedException {log.info(开始执行任务 s);long startTime System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long endTime System.currentTimeMillis();log.info(完成任务 s 耗时 (endTime - startTime) 毫秒);}}
第三步测试类添加同步调用的测试方法 SystemApplicationTests.java Slf4j
SpringBootTest(classes SystemApplication.class)
class SystemApplicationTests {Resourceprivate SyncTask syncTask;Testpublic void syncTest() throws InterruptedException {long startTime System.currentTimeMillis();syncTask.one();syncTask.two();syncTask.three();long endTime System.currentTimeMillis();log.info(任务总耗时 (endTime - startTime) 毫秒);}}异步调用
第四步添加异步调用业务逻辑 AsyncTask.java /** 异步调用* author DUHAOLIN* date 2024/10/17/
Slf4j
Component
public class AsyncTask {public static Random random new Random();Asyncpublic void one() throws InterruptedException {commonTask(一);}Asyncpublic void two() throws InterruptedException {commonTask(二);}Asyncpublic void three() throws InterruptedException {commonTask(三);}public void commonTask(String s) throws InterruptedException {log.info(开始执行任务 s);long startTime System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long endTime System.currentTimeMillis();log.info(完成任务 s 耗时 (endTime - startTime) 毫秒);}}
第五步测试类添加异步调用的测试方法 SystemApplicationTests.java Resource
private AsyncTask asyncTask;Test
public void asyncTest() throws InterruptedException {long startTime System.currentTimeMillis();asyncTask.one();asyncTask.two();asyncTask.three();long endTime System.currentTimeMillis();log.info(任务总耗时 (endTime - startTime) 毫秒);
}异步回调常用
第六步添加异步回调业务逻辑 AsyncCallBackTask.java package com.lm.system.task;import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;import java.util.Random;package com.lm.system.task;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;/** 异步回调* author DUHAOLIN* date 2024/10/17*/
Slf4j
Component
public class AsyncCallBackTask {public static Random random new Random();Asyncpublic CompletableFutureString one() throws InterruptedException {log.info(开始执行任务一);long startTime System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long endTime System.currentTimeMillis();log.info(完成任务一耗时 (endTime - startTime) 毫秒);return CompletableFuture.completedFuture(任务一执行完成);}Asyncpublic CompletableFutureString two() throws InterruptedException {log.info(开始执行任务二);long startTime System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long endTime System.currentTimeMillis();log.info(完成任务二耗时 (endTime - startTime) 毫秒);return CompletableFuture.completedFuture(任务二执行完成);}Asyncpublic CompletableFutureString three() throws InterruptedException {log.info(开始执行任务三);long startTime System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long endTime System.currentTimeMillis();log.info(完成任务三耗时 (endTime - startTime) 毫秒);return CompletableFuture.completedFuture(任务三执行完成);}}
第七步测试类添加异步回调的测试方法 SystemApplicationTests.java Resourceprivate AsyncCallBackTask asyncCallBackTask;Testpublic void asyncCallBackTaskTest() throws InterruptedException {long startTime System.currentTimeMillis();CompletableFutureString one asyncCallBackTask.one();CompletableFutureString two asyncCallBackTask.two();CompletableFutureString three asyncCallBackTask.three();CompletableFuture.allOf(one, two, three).join();long endTime System.currentTimeMillis();log.info(任务总耗时 (endTime - startTime) 毫秒);}效果图
同步调用 异步调用 执行完异步调用只有任务的部分相关输出任务的执行顺序也是乱序的。 异步回调
异步任务线程池配置
介绍
当我们用异步调用或异步回调进行并发操作时加速了任务的执行效率但是如果只是直接简单的创建来使用可能会碰到一些问题和风险。当接口被频繁调用异步任务创建的数量达到一定量级可能会导致内存溢出此时我们就需要对创建异步任务的操作加上线程池的相关配置。 queueCapacity缓冲队列的容量默认为INT的最大值2的31次方-1 maxSize允许的最大线程数默认为INT的最大值2的31次方-1
具体配置 application.yml spring:task:execution:pool:core-size: 2 #线程池创建时的初始化线程数默认为8max-size: 5 #线程池的最大线程数默认为int最大值queue-capacity: 10 #用来缓冲执行任务的队列默认为int最大值keep-alive: 60s #线程终止前允许保持空闲的时间默认为60sallow-core-thread-timeout: true #是否允许核心线程超时shutdown:await-termination: false #是否等待剩余任务完成后才关闭应用await-termination-period: #等待剩余任务完成的最大时间thread-name-prefix: task- #线程名的前缀设置好了之后可以方便我们在日志中查看处理任务所在的线程池application.Properties spring.task.execution.pool.core-size2
spring.task.execution.pool.max-size5
spring.task.execution.pool.queue-capacity10
spring.task.execution.pool.keep-alive60s
spring.task.execution.pool.allow-core-thread-timeouttrue
spring.task.execution.shutdown.await-terminationfalse
spring.task.execution.shutdown.await-termination-period
spring.task.execution.thread-name-prefixtask-效果图
再次执行异步回调方法得到如下效果图。 当前配置的初始化线程数为2最大线程数为5缓存队列为10只有当缓存队列满且当前线程数小于最大线程数时才会申请新的线程来执行任务如缓存队列为11初始化线程数为2最大线程数为5。
项目结构图 参考资料
Spring Boot 2.x基础教程使用Async实现异步调用【https://www.didispace.com/spring-boot-2⁄8-3-async-1.html】 Spring Boot 2.x基础教程配置Async异步任务的线程池【https://www.didispace.com/spring-boot-2⁄8-3-async-2.html】
- 上一篇: 东莞网站设计价格网站开发帐务处理
- 下一篇: 东莞网站搜索排名ftp 迁移 网站
相关文章
-
东莞网站设计价格网站开发帐务处理
东莞网站设计价格网站开发帐务处理
- 技术栈
- 2026年03月21日
-
东莞网站设计方案php cms网站建设
东莞网站设计方案php cms网站建设
- 技术栈
- 2026年03月21日
-
东莞网站模板呼伦贝尔网站开发
东莞网站模板呼伦贝尔网站开发
- 技术栈
- 2026年03月21日
-
东莞网站搜索排名ftp 迁移 网站
东莞网站搜索排名ftp 迁移 网站
- 技术栈
- 2026年03月21日
-
东莞网站推广费用网站设计与开发公司
东莞网站推广费用网站设计与开发公司
- 技术栈
- 2026年03月21日
-
东莞网站推广哪里好个人网页模板
东莞网站推广哪里好个人网页模板
- 技术栈
- 2026年03月21日






