SpringBoot + kotlin 协程小记

server/2024/9/23 5:19:11/

前言:

Kotlin 协程是基于 Coroutine 实现的,其设计目的是简化异步编程。协程提供了一种方式,可以在一个线程上写起来像是在多个线程中执行。

协程的基本概念:

  • 协程是轻量级的,不会创建新的线程。

  • 协程会挂起当前的协程,而不会阻塞线程。

  • 协程可以在suspend函数中暂停执行,并在暂停点恢复执行。

一、引包

${kotlin.version} : <kotlin.version>1.8.21</kotlin.version>

注意:org.jetbrains.kotlinx 最新在1.6.0

        <dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-stdlib-jdk8</artifactId><version>${kotlin.version}</version></dependency><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-reflect</artifactId><version>${kotlin.version}</version></dependency><dependency><groupId>org.jetbrains.kotlinx</groupId><artifactId>kotlinx-coroutines-core</artifactId><version>1.6.0</version></dependency><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-test</artifactId><version>${kotlin.version}</version><scope>test</scope></dependency>

二、 interface 定义

import com.baomidou.mybatisplus.extension.service.IService
import com.zc.bean.PriceBean
import org.springframework.stereotype.Repository@Repository
interface TestKotlinService : IService<PriceBean> {//suspend 协程方法的定义suspend fun test(): Stringfun te()
}

三、Ipml 实现

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.zc.bean.PriceBean
import com.zc.mapper.TestKotlinMapper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Service/*** @author zc* @date 2024/4/18 9:56* @desc*/
@Service
@Slf4j
open class TestKotlinServcieImpl : ServiceImpl<TestKotlinMapper, PriceBean>(), TestKotlinService{//withContext 切换到指定的线程(IO线程)override suspend fun test() = withContext(Dispatchers.IO){delay(10000)println("suspend")return@withContext "success"}override fun te(){println("test")}
}

四、controller 

/*** @author zc* @date 2024/4/18 10:38* @desc*/
@RestController
@RequestMapping("/price")
@Tag(name = "kotlin")
class TestKotlinController {@Autowiredprivate lateinit var testKotlinService: TestKotlinService;@GetMapping("test")fun testScope(){println("start")testKotlinService.te()//创建协程,并在io线程上执行val coroutineScope = CoroutineScope(Dispatchers.IO)coroutineScope.launch {//async/await获取返回值
//            val result =  async {  testKotlinService.test() }.await()val result =withContext(Dispatchers.IO) { testKotlinService.test() }println("result: $result")}println("end")}
}

五、测试

调用swagger 接口测试,在等待10秒后打印出suspend result: success,异步调用成功

六、学习文档:

文档 · Kotlin 官方文档 中文版


http://www.ppmy.cn/server/12304.html

相关文章

【笔记django】创建一个app

创建app 错误 raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Cannot import rules. Check that dvadmin.rules.apps.RulesConfig.name is correct.原因 刚创建的rules的app被手动移动到了dvadmin目录下 而dvadmin/rules/apps.py的内容还是&…

Elasticsearch集群部署(Linux)

1. 准备环境 这里准备三台Linux虚拟机&#xff0c;用于配置Elasticsearch集群和部署可视化工具Kibana。 角色IP域名集群名称节点名称版本操作系统ES192.168.243.100linux100cluster-eses-node-1007.12.0CentOS 7192.168.243.101linux101cluster-eses-node-101192.168.243.102…

测绘管理与法律法规 | 测绘资质管理办法 | 学习笔记

目录 一、测绘资质概述 二、测绘资质分类与等级 三、审批与管理 四、申请条件 五、审批程序 六、测绘资质证书 七、监督管理 八、违规处理 九、特殊规定 十、审批受理时间要点补充 1. 审批机关决定是否受理的时间 2. 审批机关作出批准与否的决定时间 3. 颁发测绘资…

【C语言】判断布尔矩阵是否自反、非自反、对称、非对称、反对称、传递与否,并计算其互补关系和逆矩阵

实验目的 Using C-language to judge whether a Boolean Matrix is reflexive, irreflexive, symmetric, asymmetric, antisymmetric, transitive, or not, and calculate the complementary relation and the inverse. 实验内容 Design the algorithm to judge whether a …

使用yolo识别模型对比两张图片并标记不同(2)

上篇文章有漏洞&#xff0c;在这里补充下&#xff0c;比如要识别第二张图相对于第一张图的违建是否拆除了 第一步旋转对其后&#xff0c;图片会有黑色的掩码&#xff0c;如果旋转角度大的话&#xff0c;没识别出来的框可能不是已经拆除了&#xff0c;而是因为黑色掩码遮挡&…

学习笔记-数据结构-线性表(2024-04-18)- 单向链表选择排序

试以单向链表为存储结构实现简单选择排序的算法。 实现递增排序&#xff0c;首先选择一个元素作为第一个比较值&#xff0c;遍历其他所有的元素&#xff0c;如果发现其他元素中有比它小的元素&#xff0c;则交换两个元素&#xff0c;这样每一趟都能找到符合要求的最小值 正经…

XiebroC2 渗透测试图形化框架

XiebroC2简介 这款工具支持多人协作的渗透测试图形化框架、支持lua插件扩展、域前置/CDN上线、自定义多个模块、自定义shellcode、文件管理、进程管理、内存加载等功能。 特点/特征 被控端&#xff08;Client&#xff09;由Golang编写&#xff0c;兼容WIndows、Linux、MacOS上…

在Linux上开启FTP服务

在Linux上设置FTP服务&#xff0c;您可以使用几种不同的FTP服务器软件&#xff0c;如vsftpd、ProFTPD或Pure-FTPd。以下是使用vsftpd&#xff08;非常安全的FTP守护程序&#xff09;的基本步骤&#xff1a; 安装 vsftpd 首先&#xff0c;需要确保vsftpd已经安装。在Debian/Ub…