kotlin协程并发/并行与串行互相切换,CoroutineScope与await
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.time.LocalTime
fun main(args: Array) {
println("${LocalTime.now()} - main start")
CoroutineScope(Dispatchers.Default).launch {
// 并发执行
this.launch {
println("${LocalTime.now()} A start")
delay(1000)
println("${LocalTime.now()} A end")
}
this.launch {
println("${LocalTime.now()} B start")
delay(1500)
println("${LocalTime.now()} B end")
}
}
println("${LocalTime.now()} - main end")
}
输出表明main退出后,A,B均没有输出。CoroutineScope(Dispatchers.IO).launch新起的任务不阻塞主main任务执行流程。
如果换成runBlocking(Dispatchers.IO):
import kotlinx.coroutines.*
import java.time.LocalTime
fun main(args: Array) {
println("${LocalTime.now()} - main start")
runBlocking(Dispatchers.IO) {
// 并发执行
this.launch {
println("${LocalTime.now()} A start")
delay(1000)
println("${LocalTime.now()} A end")
}
this.launch {
println("${LocalTime.now()} B start")
delay(1500)
println("${LocalTime.now()} B end")
}
}
println("${LocalTime.now()} - main end")
}
看一下async
import kotlinx.coroutines.*
import java.time.LocalTime
fun main(args: Array) {
println("${LocalTime.now()} - main start")
CoroutineScope(Dispatchers.IO).launch() {
// 并发执行
this.async {
println("${LocalTime.now()} A start")
delay(1000)
println("${LocalTime.now()} A end")
}
this.async {
println("${LocalTime.now()} B start")
delay(1500)
println("${LocalTime.now()} B end")
}
}
println("${LocalTime.now()} - main end")
}
如果main线程休息1000ms:
import kotlinx.coroutines.*
import java.time.LocalTime
fun main(args: Array) {
println("${LocalTime.now()} - main start")
CoroutineScope(Dispatchers.IO).launch() {
// 并发执行
this.async {
println("${LocalTime.now()} A start")
delay(1000)
println("${LocalTime.now()} A end")
}
this.async {
println("${LocalTime.now()} B start")
delay(1500)
println("${LocalTime.now()} B end")
}
}
Thread.sleep(1000)
println("${LocalTime.now()} - main end")
}
import kotlinx.coroutines.*
import java.time.LocalTime
fun main(args: Array) {
println("${LocalTime.now()} - main start")
runBlocking {
CoroutineScope(Dispatchers.IO).launch() {
val task1 = this.async {
println("${LocalTime.now()} A start")
delay(1000)
println("${LocalTime.now()} A end")
"task1 return"
}
val task2 = this.async {
println("${LocalTime.now()} B start")
delay(1500)
println("${LocalTime.now()} B end")
"task2 return"
}
val t1 = task1.await()
println("${LocalTime.now()} $t1")
val t2 = task2.await()
println("${LocalTime.now()} $t2")
}
}
println("${LocalTime.now()} - main end")
}
总体上CoroutineScope(Dispatchers.IO).launch()的意义在于新起一个线程,不阻塞主main线程,由于
理解await
import kotlinx.coroutines.*
import java.time.LocalTime
fun main(args: Array) {
println("${LocalTime.now()} - main start")
runBlocking {
//launch() {
val task1 = this.async {
println("${LocalTime.now()} A start")
delay(3000)
println("${LocalTime.now()} A end")
"task1 return"
}
val task2 = this.async {
println("${LocalTime.now()} B start")
delay(1000)
println("${LocalTime.now()} B end")
"task2 return"
}
val t1 = task1.await()
println("${LocalTime.now()} $t1")
val t2 = task2.await()
println("${LocalTime.now()} $t2")
//}
}
println("${LocalTime.now()} - main end")
}
虽然task2很快就完成,因为await,所以必须等待task1返回。
kotlin协程async与await_zhangphil的博客-CSDN博客runBlocking 内部启动的3个协程做耗时操作,从输出可以看到3个协程交叉并发执行,runBlocking 会等到3个协程执行结束后才退出,输出结果有明确先后顺序。一般编程的技法,比如,在Android中,假设在主线程中实现了一个函数,但该函数是耗时操作,毫无疑问,需要将这个函数的实现切入非主线程中操作,那么可以设计一种托管的函数,在托管的函数里面干脏活,处理完成后,把结果抛到主线程。结果1-a: 5 - tid:22。结果1-b: 5 - tid:24。结果2-a: 9 - tid:22。https://blog.csdn.net/zhangphil/article/details/129268399
https://zhangphil.blog.csdn.net/article/details/129265638https://zhangphil.blog.csdn.net/article/details/129265638
kotlin协程、线程切换,函数方法委托_zhangphil的博客-CSDN博客runBlocking 内部启动的3个协程做耗时操作,从输出可以看到3个协程交叉并发执行,runBlocking 会等到3个协程执行结束后才退出,输出结果有明确先后顺序。一般编程的技法,比如,在Android中,假设在主线程中实现了一个函数,但该函数是耗时操作,毫无疑问,需要将这个函数的实现切入非主线程中操作,那么可以设计一种托管的函数,在托管的函数里面干脏活,处理完成后,把结果抛到主线程。结果1-a: 5 - tid:22。结果1-b: 5 - tid:24。结果2-a: 9 - tid:22。https://blog.csdn.net/zhangphil/article/details/130161705
https://zhangphil.blog.csdn.net/article/details/129250518https://zhangphil.blog.csdn.net/article/details/129250518