您的位置:首页 > 教育 > 培训 > seog_佛山公司网站推广外包服务_夫唯seo_网站推广排名

seog_佛山公司网站推广外包服务_夫唯seo_网站推广排名

2025/7/13 17:15:36 来源:https://blog.csdn.net/jiay2/article/details/143222771  浏览:    关键词:seog_佛山公司网站推广外包服务_夫唯seo_网站推广排名
seog_佛山公司网站推广外包服务_夫唯seo_网站推广排名

coroutines并不在kotlin的标准库中,但kotlin提供了协程支持

使用协程,先引入协程包

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0")

先来一个简单的协程例子:

fun main() {runBlocking {launch {delay(1000L) println("LiYunLong!")}println("Hello, ")}
}

runBlocking函数是一个协程构建器,主要作用是将当前函数中的非协程代码协程化,

runBlocking会创建一个CoroutineScope作用域,kotlin协程遵循结构化并发的原则,这意味着新的协程只能在特定的CoroutineScope中启动,

该CoroutineScope限定了协程的生存期外部作用域在其所有子协程完成之前不能完成。
结构化并发性还确保正确报告代码中的任何错误

launch函数也是一个协程构建器
launch的作用是为函数中的代码启动一个并发线程
launch函数只能在CoroutineScope作用域中运行,因此,协程代码须放在runBlocking函数中

dealy是一个特殊的暂停函数(suspend),将当前协程代码延时挂起,挂起的协程代码不会阻塞线程,但允许其它协程代码运行

上面代码运行结果:

Hello, 
World!

kotlin自定义协程函数需要使用suspend

fun main() {runBlocking {launch {doName()}println("Hello, ")}
}
private suspend fun doName() {delay(1000L)println("LiYunlong!")
}
fun main() {runBlocking {doName()}
}private  suspend fun doName() {coroutineScope {launch {delay(1000L)println("LiYunlong!")}println("Hello, ")}
}

多个协程进程会同时运行,具体延时时间取决于delay()的值参

fun main() {runBlocking {doName()}
}private  suspend fun doName() {coroutineScope {launch {delay(3000L)println("LiYunLong")}launch {delay(2000L)println("YunFeiXiong")}launch {delay(1000L)println("DaBiao")}launch {delay(2000L)println("ZhengWei")}println("Hello, ")}
}

YunFeiXiong和ZhengWei的delay时间都是2000L,因此YunFeiXiong和ZhengWei会同时弹出

DaBiao
YunFeiXiong
ZhengWei
LiYunLong

协程比进程占用更少的资源,并且协程拥有更少哦的资源限制

    coroutineScope {repeat(5000000){//同时启用5000000个协程进程轻轻松松launch {delay(1000)println("'._")}}}

和launch一样async也是一个协程构建器
不同的是,launch不返回特定结果,而async会返回一个期望的结果

下面以OkHttp爬取一个网页为例:

fun main() {runBlocking {getBaidu()}}private suspend fun getBaidu(){coroutineScope {launch {delay(1000L)request("https://baidu.com","BAIDUID=*; PSTM=1729731690")}println(async {delay(1000L)// 请把Cookie换成自己浏览器捕捉到的request("https://baijiahao.baidu.com/s?id=1813752803356680783&wfr=spider&for=pc","BAIDUID=*; PSTM=1729731690")}.await())println(launch {delay(1000L)request("https://baijiahao.baidu.com/s?id=1813752803356680783&wfr=spider&for=pc","BAIDUID=*; PSTM=1729731690")})}
}private fun request(url:String, cookie:String):String{val client = OkHttpClient()val request = Request.Builder().url(url).addHeader("Cookie", cookie).build()val response:Response = client.newCall(request).execute()return response.body?.string().toString()
}

执行上面代码,只有async会打印返回结果,而launch函数则只是默默付出

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com