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

造纸公司网站建设西安网约车平台

造纸公司网站建设,西安网约车平台,管理是什么,金坛常州做网站Git仓库 https://gitee.com/Lin_DH/system 介绍 同步调用:指程序在执行时,调用方需要等待函数调用返回结果后,才能继续执行下一步操作,是一种阻塞式调用。 异步调用:指程序在执行时,调用方在调用函数后立…

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 CompletableFuture<String> 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 CompletableFuture<String> 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 CompletableFuture<String> 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();CompletableFuture<String> one = asyncCallBackTask.one();CompletableFuture<String> two = asyncCallBackTask.two();CompletableFuture<String> 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-size=2
spring.task.execution.pool.max-size=5
spring.task.execution.pool.queue-capacity=10
spring.task.execution.pool.keep-alive=60s
spring.task.execution.pool.allow-core-thread-timeout=true
spring.task.execution.shutdown.await-termination=false
spring.task.execution.shutdown.await-termination-period=
spring.task.execution.thread-name-prefix=task-

效果图

再次执行异步回调方法,得到如下效果图。
当前配置的初始化线程数为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】

http://www.yidumall.com/news/95056.html

相关文章:

  • 西昌有哪些做网站的公司优化网站内容
  • 个人网站logo生成百度推广平台登录
  • 做网站注册哪类商标百度在线客服系统
  • 建立网站需要河南新站关键词排名优化外包
  • 乐山网站制作公司小程序开发流程
  • 外贸网站好做吗小视频关键词汇总
  • 论述网站建设的主要步骤seo前线
  • 补单平台搭建广州seo网站推广优化
  • 家具网站建设策划书seo网站优化平台
  • 哪里有做空包网站的许昌seo推广
  • 查做外贸客户的网站营销网站策划方案
  • 搜狗网址大全下载安装山西优化公司
  • 分析网站北京seo营销培训
  • 平面设计网站导航百度上怎么注册店铺地址
  • 手机端网站动效类怎么做谷歌seo外链
  • 邵阳网页制作淮北seo
  • 阳泉那有做网站的百度销售系统登录
  • 免费代理网址seo实战
  • 公司网站建设作用网页设计模板免费网站
  • 商城网站建设是 什么青岛专业网站制作
  • 网站建设中英文表述外贸快车
  • 湘西泸溪网站建设推广app赚钱的平台
  • 苹果网站模版磁力猫引擎
  • 阿里巴巴上做网站要多少钱产品推广朋友圈文案
  • 易商官方网站现在疫情怎么样了最新消息
  • 网站建设与制百度排名优化软件
  • 艺术网站源码阿里云域名注册入口官网
  • 做网站包含的技术seo查询 站长工具
  • 向国外卖货的电商平台简述什么是seo
  • 安徽网站建设案例百度首页入口