- Learning RxJava
- Thomas Nield
- 135字
- 2021-07-02 22:22:53
Observable.never()
A close cousin of Observable.empty() is Observable.never(). The only difference between them is that it never calls onComplete(), forever leaving observers waiting for emissions but never actually giving any:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable<String> empty = Observable.never();
empty.subscribe(System.out::println,
Throwable::printStackTrace,
() -> System.out.println("Done!"));
sleep(5000);
}
public static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
This Observable is primarily used for testing and not that often in production. We have to use sleep() here just like Observable.interval() because the main thread is not going to wait for it after kicking it off. In this case, we just use sleep() for five seconds to prove that no emissions are coming from it. Then, the application will quit.
推薦閱讀
- Oracle從新手到高手
- AngularJS深度剖析與最佳實踐
- 面向對象程序設計(Java版)
- EPLAN實戰設計
- Learning Vaadin 7(Second Edition)
- Learning Concurrent Programming in Scala
- 深入分布式緩存:從原理到實踐
- Python爬蟲、數據分析與可視化:工具詳解與案例實戰
- Spring 5 Design Patterns
- Node.js區塊鏈開發
- SQL Server 2014數據庫設計與開發教程(微課版)
- Java程序設計(項目教學版)
- Analytics for the Internet of Things(IoT)
- 秒懂算法:用常識解讀數據結構與算法
- Vue.js項目開發實戰