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反应式编程
34
2
在语义上,它与前面的代码相同,但是更加简短。除此之外,相对于
create(),它还有一
些其他的优点,稍后会介绍。
2.4.3
 计时
timer()
interval()
我们已经介绍了 Observable 如何自行创建线程,但它在
RxJava
中并不是最好的模式。后
面的章节会介绍调度器,但是我们先来了解一下两个非常有用的操作符,也就是 timer()
interval(),它们在底层会用到线程。前者只是简单地创建一个 Observable,在特定的
延迟后发布一个 long 类型的零值,然后完成。
Observable
.timer(1, TimeUnit.SECONDS)
.subscribe((Long zero) -> log(zero));
虽然看起来很傻,但是 timer() 非常有用。基本上,它就是一个异步版本的 Thread.
sleep()。我们创建了一个 Observable 并通过 subscribe() 订阅它,而不是阻塞当前线程。
学习如何将简单的 Observable 组合为更复杂的计算形式之后,它将变得更加重要。固定的
值零(在 zero 变量中)只是一个约定,并没有任何特殊的含义。不过,引入 interval()
之后,就会更有意义了。interval() 会生成一个 long 类型数字的序列,从零开始,每个数
字之间有固定的时间间隔。
Observable
.interval(1_000_000 / 60, MICROSECONDS)
.subscribe((Long ...
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