collect是suspend函数,所以后续代码因为协程挂起不会继续执行。

所以上面代码可能会不符合预期,因为第一个collect不走完第二个走不到。

正确的写法是为每个collect单独起一个协程。

或者使用launchIn,写法更加优雅 。

launchIn不会挂起协程,所以与RxJava的subscribe更加接近。

通过名字可以感觉出来launchIn只不过是之前例子中launch的一个链式调用的语法糖。

flowOf

flowOf类似于Observable.just(),需要注意flowOf内的内容是立即执行的,不受flowOn影响。

希望calculate()运行在IO,可以使用flow{ }。

flatMapMerge

flatMapMerge类似RxJava的flatMap 。

如上,2个item各自flatMap成2个item,即一共发射了4条数据,日志输出如下:

inner: pool-2-thread-2 @coroutine#4

inner: pool-2-thread-3 @coroutine#5

inner: pool-2-thread-3 @coroutine#5

inner: pool-2-thread-2 @coroutine#4

collect: pool-1-thread-2 @coroutine#2

collect: pool-1-thread-2 @coroutine#2

collect: pool-1-thread-2 @coroutine#2

collect: pool-1-thread-2 @coroutine#2

通过日志我们发现flowOn虽然写在flatMapMerge外面,inner的日志却可以打印在多个线程上(都来自pool2线程池),这与flatMap是不同的,同样场景下flatMap只能运行在线程池的固定线程上。

如果将flowOn写在flatMapMerge内部。

结果如下:

inner: pool-2-thread-2 @coroutine#6

inner: pool-2-thread-1 @coroutine#7

inner: pool-2-thread-2 @coroutine#6

inner: pool-2-thread-1 @coroutine#7

collect: pool-1-thread-3 @coroutine#2

collect: pool-1-thread-3 @coroutine#2

collect: pool-1-thread-3 @coroutine#2

collect: pool-1-thread-3 @coroutine#2

inner仍然打印在多个线程,flowOn无论写在flatMapMerge内部还是外部,对flatMapMerge内的处理没有区别。

但是flatMapMerge之外还是有区别的,看下面两段代码 。

通过颜色可以知道flowOn影响的范围,向上追溯到flowOf为止。

Summary

===================================================================

RxJava的Observable与Coroutine的Flow都支持线程切换,相关API的对比如下:

最后通过一个例子看一下如何将代码从RxJava迁移到Flow。

RxJava

RxJava代码如下:

使用到的Schedulers定义如下:

代码执行结果:

1: pool-1-thread-1

1: pool-1-thread-1

1: pool-1-thread-1

2: pool-3-thread-1

2: pool-3-thread-1

2: pool-3-thread-1

inner 1: pool-4-thread-1

inner 1: pool-4-thread-2

inner 1: pool-4-thread-1

inner 1: pool-4-thread-1

inner 1: pool-4-thread-2

inner 1: pool-4-thread-2

inner 1: pool-4-thread-3

inner 2: pool-5-thread-1

inner 2: pool-5-thread-2

3: pool-5-thread-1

inner 2: pool-5-thread-2

inner 1: pool-4-thread-3

inner 2: pool-5-thread-2

inner 2: pool-5-thread-3

3: pool-5-thread-1

3: pool-5-thread-1

3: pool-5-thread-1

end: pool-6-thread-1

end: pool-6-thread-1

inner 1: pool-4-thread-3

end: pool-6-thread-1

3: pool-5-thread-1

inner 2: pool-5-thread-1

3: pool-5-thread-1

inner 2: pool-5-thread-3

inner 2: pool-5-thread-1

end: pool-6-thread-1

3: pool-5-thread-3

3: pool-5-thread-3

end: pool-6-thread-1

inner 2: pool-5-thread-3

3: pool-5-thread-3

end: pool-6-thread-1

end: pool-6-thread-1

end: pool-6-thread-1

end: pool-6-thread-1

代码较长,通过颜色标记法帮我们理清线程关系。

上色后一目了然了,需要特别注意的是由于flatMap中切换了数据源的同时切换了线程,所以打印 3的线程不是s2 而是 s4

Flow

首先创建对应的Dispatcher。

然后将代码换成Flow的写法,主要遵循下列原则。

  • RxJava通过observeOn切换后续代码的线程。

  • Flow通过flowOn切换前置代码的线程。

打印结果如下:

1: pool-1-thread-1 @coroutine#6

1: pool-1-thread-1 @coroutine#6

1: pool-1-thread-1 @coroutine#6

2: pool-2-thread-2 @coroutine#5

2: pool-2-thread-2 @coroutine#5

2: pool-2-thread-2 @coroutine#5

inner 1: pool-3-thread-1 @coroutine#10

inner 1: pool-3-thread-2 @coroutine#11

inner 1: pool-3-thread-3 @coroutine#12

inner 1: pool-3-thread-2 @coroutine#11

inner 1: pool-3-thread-3 @coroutine#12

inner 2: pool-4-thread-3 @coroutine#9

inner 1: pool-3-thread-1 @coroutine#10

inner 1: pool-3-thread-3 @coroutine#12

inner 1: pool-3-thread-2 @coroutine#11

inner 2: pool-4-thread-1 @coroutine#7

inner 2: pool-4-thread-2 @coroutine#8

inner 2: pool-4-thread-1 @coroutine#7

inner 2: pool-4-thread-3 @coroutine#9

inner 1: pool-3-thread-1 @coroutine#10

3: pool-4-thread-1 @coroutine#3

inner 2: pool-4-thread-3 @coroutine#9

inner 2: pool-4-thread-2 @coroutine#8

end: pool-5-thread-1 @coroutine#2

3: pool-4-thread-1 @coroutine#3

inner 2: pool-4-thread-2 @coroutine#8

3: pool-4-thread-1 @coroutine#3

end: pool-5-thread-1 @coroutine#2

3: pool-4-thread-1 @coroutine#3

end: pool-5-thread-1 @coroutine#2

end: pool-5-thread-1 @coroutine#2

3: pool-4-thread-1 @coroutine#3

3: pool-4-thread-1 @coroutine#3

end: pool-5-thread-1 @coroutine#2

end: pool-5-thread-1 @coroutine#2

3: pool-4-thread-1 @coroutine#3

3: pool-4-thread-1 @coroutine#3

end: pool-5-thread-1 @coroutine#2

end: pool-5-thread-1 @coroutine#2

inner 2: pool-4-thread-1 @coroutine#7

3: pool-4-thread-1 @coroutine#3

end: pool-5-thread-1 @coroutine#2

从日志可以看到,1、2、3的时序性以及inner1和inner2的并发性与RxJava的一致。

FIN

===============================================================

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

架构师筑基包括哪些内容

我花了将近半个月时间将:深入 Java 泛型.、注解深入浅出、并发编程.、数据传输与序列化、Java 虚拟机原理、反射与类加载、高效 IO、Kotlin项目实战等等Android架构师筑基必备技能整合成了一套系统知识笔记PDF,相信看完这份文档,你将会对这些Android架构师筑基必备技能有着更深入、更系统的理解。

由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容

注:资料与上面思维导图一起看会更容易学习哦!每个点每个细节分支,都有对应的目录内容与知识点!



这份资料就包含了所有Android初级架构师所需的所有知识!

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!

深入 Java 泛型.、注解深入浅出、并发编程.、数据传输与序列化、Java 虚拟机原理、反射与类加载、高效 IO、Kotlin项目实战等等Android架构师筑基必备技能整合成了一套系统知识笔记PDF,相信看完这份文档,你将会对这些Android架构师筑基必备技能有着更深入、更系统的理解。

由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容

注:资料与上面思维导图一起看会更容易学习哦!每个点每个细节分支,都有对应的目录内容与知识点!

[外链图片转存中…(img-7M3mT5sy-1712306633710)]
[外链图片转存中…(img-iH1tANyJ-1712306633710)]
这份资料就包含了所有Android初级架构师所需的所有知识!

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐