栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Kotlin Flow实现线程切换

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Kotlin Flow实现线程切换

Kotlin Flow实现线程切换
    • flowOn方法实现线程切换
    • Log结果如下
    • 注意:flowOn方法一般跟在flow耗时流后面,不然可能会造成其他方法也在子线程

flowOn方法实现线程切换
 private fun flowOn() {
        lifecycleScope.launch {//主线程
            simpleFlowOn()
                .flowOn(Dispatchers.IO)//子线程执行,耗时操作
                .onStart {//最先开始
                    LogUtils.d("Thread is ${Thread.currentThread().name} onStart")
                }
                .onEach {
                    LogUtils.d("Thread is ${Thread.currentThread().name} onEach")
                }
                .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")
                }
        }
    }

 //实现一个流,每秒发送一个数据
    private fun simpleFlowOn(): Flow = flow {
        for (i in 1..3) {
            delay(1000)
            LogUtils.d("Thread is ${Thread.currentThread().name} Emitting $i")
//            if (i == 3) throw IllegalStateException("状态错误")
            emit(i)
        }
    }

Log结果如下
	//在主线程开始onStart
    D/FlowFragment: Thread is main onStart
    //发送在子线程
    D/FlowFragment: Thread is DefaultDispatcher-worker-1 Emitting 1
    D/FlowFragment: Thread is main onEach
    //接收在主线程
    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
    //最后完成在主线程
    D/FlowFragment: Thread is main onCompletion
注意:flowOn方法一般跟在flow耗时流后面,不然可能会造成其他方法也在子线程

看下面的方法,将flowOn放在onStart、onEach后面

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")
                }
        }
    }

打印如下,onStart,onEach方法执行都在子线程了:

	//onStart,onEach方法执行都在子线程了
    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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/270886.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号