Skip to Content
RxJava反应式编程
book

RxJava反应式编程

by Tomasz Nurkiewicz, Ben Christensen
December 2019
Beginner to intermediate
330 pages
10h 7m
Chinese
Posts & Telecom Press
Content preview from RxJava反应式编程
62
3
3.2.1
 使用
merge()
将多个
Observable
合并为一个
还记得
3.1.2
节中的 Observable<LicensePlate> recognize(CarPhoto photo) 方法吗?这个
方法会异步地根据 CarPhoto 识别 LicensePlate。我们曾经简单地提过这样的流可能会同时
使用多个算法,有的可能会更快,有的可能会更精确。但是,我们并不想将这些细节暴露
给外部世界,只是想有一个包含渐进式优化结果的流,这个结果来源于各个算法,从最快
的到最精确的。
假设现在有三个支持
RxJava
的算法,每个算法都使用 Observable 进行了很好的封装。当
然,每个算法可能会生成零个到无穷个结果。
Observable<LicensePlate> fastAlgo(CarPhoto photo) {
//速度快但质量差
}
Observable<LicensePlate> preciseAlgo(CarPhoto photo) {
//精确但代价高昂
}
Observable<LicensePlate> experimentalAlgo(CarPhoto photo) {
//不可预测,但是可运行
}
我们想要达到的效果是同时运行这三个算法(参见
4.9.2
节,了解
RxJava
处理并发的更多
细节)并尽快接收结果。我们并不关心是哪个算法发布了事件,只想要捕获所有的事件并
将它们聚集到一个流中。这就是 merge() 操作符能实现的功能,如下所示。
Observable<LicensePlate> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Java并发编程

Java并发编程

道格拉斯·施密特
Spark快速大数据分析(第2版)

Spark快速大数据分析(第2版)

Jules S. Damji, Brooke Wenig, Tathagata Das, Denny Lee

Publisher Resources

ISBN: 9787115524003