当前位置:网站首页>Kotlin flow realizes thread switching
Kotlin flow realizes thread switching
2022-04-22 07:39:00 【Xu Sanduo 2020】
Kotlin Flow Implement thread switching
flowOn Method to realize thread switching
private fun flowOn() {
lifecycleScope.launch {
// The main thread
simpleFlowOn()
.flowOn(Dispatchers.IO)// Sub thread execution , The long-running
.onStart {
// Start first
LogUtils.d("Thread is ${
Thread.currentThread().name} onStart")
}
.onEach {
LogUtils.d("Thread is ${
Thread.currentThread().name} onEach")
}
.catch {
exception ->// Abnormal reception
LogUtils.d("Thread is ${
Thread.currentThread().name} exception $exception")
}
.onCompletion {
// The whole flow is complete
LogUtils.d("Thread is ${
Thread.currentThread().name} onCompletion")
}
.collect {
// Data reception
LogUtils.d("Thread is ${
Thread.currentThread().name} collect $it")
}
}
}
// Implement a stream , Send one data per second
private fun simpleFlowOn(): Flow<Int> = flow {
for (i in 1..3) {
delay(1000)
LogUtils.d("Thread is ${
Thread.currentThread().name} Emitting $i")
// if (i == 3) throw IllegalStateException(" Status error ")
emit(i)
}
}
Log give the result as follows
// At the beginning of the main thread onStart
D/FlowFragment: Thread is main onStart
// Send in sub thread
D/FlowFragment: Thread is DefaultDispatcher-worker-1 Emitting 1
D/FlowFragment: Thread is main onEach
// Received in the main thread
D/Collect: Thread is main collect 1
D/FlowFragment: Thread is DefaultDispatcher-worker-1 Emitting 2
D/FlowFragment: Thread is main onEach
D/Collect: Thread is main collect 2
D/FlowFragment: Thread is DefaultDispatcher-worker-1 Emitting 3
D/FlowFragment: Thread is main onEach
D/Collect: Thread is main collect 3
// Finally, it is completed in the main thread
D/FlowFragment: Thread is main onCompletion
Be careful :flowOn The method usually follows flow Behind the time-consuming stream , Otherwise, other methods may also be in the sub thread
Look at the following method , take flowOn Put it in onStart、onEach Back
private fun flowOn() {
lifecycleScope.launch {
simpleFlowOn()
.onStart {
LogUtils.d("Thread is ${
Thread.currentThread().name} onStart")
}
.onEach {
LogUtils.d("Thread is ${
Thread.currentThread().name} onEach")
}
.flowOn(Dispatchers.IO)
.catch {
exception ->
LogUtils.d("Thread is ${
Thread.currentThread().name} exception $exception")
}
.onCompletion {
LogUtils.d("Thread is ${
Thread.currentThread().name} onCompletion")
}
.collect {
LogUtils.d("Thread is ${
Thread.currentThread().name} collect $it")
}
}
}
Print as follows ,onStart,onEach Method execution is in the sub thread :
//onStart,onEach Method execution is in the sub thread
D/FlowFragment: Thread is DefaultDispatcher-worker-1 onStart
D/FlowFragment: Thread is DefaultDispatcher-worker-1 Emitting 1
D/FlowFragment: Thread is DefaultDispatcher-worker-1 onEach
D/Collect: Thread is main collect 1
D/FlowFragment: Thread is DefaultDispatcher-worker-1 Emitting 2
D/FlowFragment: Thread is DefaultDispatcher-worker-1 onEach
D/Collect: Thread is main collect 2
D/FlowFragment: Thread is DefaultDispatcher-worker-1 Emitting 3
D/FlowFragment: Thread is DefaultDispatcher-worker-1 onEach
D/Collect: Thread is main collect 3
D/FlowFragment: Thread is main onCompletion
版权声明
本文为[Xu Sanduo 2020]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220617337121.html
边栏推荐
猜你喜欢

E. Figure skiing (string sorting / check-in) (Game 5 of 2021 training League warm-up training competition)

The best way to learn

HDU Ice_ Cream's world I

Detailed explanation of linked list exercises

Explanation and use of interface

任务段&任务门

Redis的设计与实现(1):了解数据结构与对象

并发编程的艺术(6):详解ReentrantLock的原理

C language | array

Bidirectional circular linked list (see details)
随机推荐
VAD 虚拟内存
Codeforces Round #779 (Div. 2)
SUCTF 2019 EasySQL
The way to learn the strongest operator (detailed explanation of C language)
Linked list problem record I
101012分页
HDU Ice_cream‘s world I (并查集判环)
Ffmpeg command (VIII). Add watermark to video
On time complexity and space complexity
Kotlin 协程Flow、StateFlow、ShareFlow
ES6模块化与Promise
系统日志收集系列
Use of ansible
Installation and configuration of Yapi (Reprint)
深入理解MySQL(5):详谈MySQL锁算法
838 · the sum of subarrays is K
Android Room数据库Like模糊查询
843 · Digital Flip
CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes)
Leetcode - 3 - (string addition, maximum number of consecutive 1 < Ⅰ Ⅲ >, maximum difficulty of the exam, deletion of the penultimate node of the linked list)