runBlocking
因为launch只在CoroutineScope中声明
runBlocking 的名字意味着运行它的线程(在本例中是主线程)在调用期间被阻塞,直到runBlocking中所有的协程{…}完成他们的执行。您经常会在应用程序的顶层看到这样使用runBlocking,而在真正的代码中却很少这样使用,因为线程是昂贵的资源,阻塞它们的效率很低,而且通常是不可取的。
runBlocking {
launch {
delay(1000)
println("word!")
}
println("Helllo")
}
Helllo
word!
提取launch{…}转换成一个单独的函数
fun main(args: Array<String>) {
runBlocking {
launch { doWorld() }
println("Helllo")
}
}
suspend fun doWorld(){
delay(1000)
println("word!")
}
Helllo
word!
任何挂起函数中使用coroutineScope
fun main(args: Array<String>) {
runBlocking {
doWorld()
}
}
suspend fun doWorld() = coroutineScope {
launch {
delay(1000L)
println("World!")
}
println("Hello")
}
Hello
World!
coroutineScope构建器可以执行多个并发操作
fun main(args: Array<String>) {
runBlocking {
doWorld()
println("Done")
}
}
suspend fun doWorld() = coroutineScope {
launch {
delay(2000L)
println("World 2")
}
launch {
delay(1000L)
println("World 1")
}
println("Hello")
}
Hello
World 1
World 2
Don
等待子协程完成,然后打印“Done”字符串
runBlocking {
val job = launch { // launch a new coroutine and keep a reference to its Job
delay(1000L)
println("World!")
}
println("Hello")
job.join() // wait until child coroutine completes
println("Done")
}
Hello
World!
Done
组合挂起函数,等拿到每个条件之后,在得出结论
fun main() = runBlocking<Unit> {
//sampleStart
val time = measureTimeMillis {
println("The answer is ${concurrentSum()}")
}
println("Completed in $time ms")
//sampleEnd
}
suspend fun concurrentSum(): Int = coroutineScope {
val one = async { doSomethingUsefulOne() }
val two = async { doSomethingUsefulTwo() }
one.await() + two.await()
}
suspend fun doSomethingUsefulOne(): Int {
delay(1000L) // 假设我们在这里做了些有用的事
return 13
}
suspend fun doSomethingUsefulTwo(): Int {
delay(1000L) // 假设我们在这里也做了些有用的事
return 29
}
The answer is 42
Completed in 1017 ms
异步流Flow倒计时
@RequiresApi(Build.VERSION_CODES.N)
fun main(args: Array<String>) {
runBlocking {
launch {
val ret = countdown(60_000, 2_000) { remianTime ->
println("剩余时间$remianTime")
}.onStart {
println("countdown start")
}.onCompletion {
println("countdown end")
}.reduce { acc, value ->
println("acc$acc value $value ")
}
println("coutdown acc ret = $ret")
}
}
}
fun <T> countdown(
duration: Long,
interval: Long,
onCountdown: suspend (Long) -> T
): Flow<T> = flow {
(duration - interval downTo 0 step interval).forEach { emit(it) }
}.onEach { delay(interval) }
.onStart { emit(duration) }
.map { onCountdown(it) }
.flowOn(Dispatchers.Default)
countdown start
剩余时间60000
剩余时间58000
acckotlin.Unit value kotlin.Unit
剩余时间56000
acckotlin.Unit value kotlin.Unit
剩余时间54000
........
acckotlin.Unit value kotlin.Unit
剩余时间2000
acckotlin.Unit value kotlin.Unit
剩余时间0
acckotlin.Unit value kotlin.Unit
countdown end
coutdown acc ret = kotlin.Unit
参考文章 :https://book.kotlincn.net/text/flow.html
Kotlin 异步 | Flow 应用场景及原理 https://juejin.cn/post/6989032238079803429