Book Image

Learning Reactive Programming with Java 8

By : Nickolay Tzvetinov
Book Image

Learning Reactive Programming with Java 8

By: Nickolay Tzvetinov

Overview of this book

Table of Contents (15 chapters)
Learning Reactive Programming with Java 8
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Chapter 6. Using Concurrency and Parallelism with Schedulers

Modern processors have multiple cores and enable many time-consuming operations to be processed faster simultaneously. The Java concurrency API (which includes threads and much more) makes it possible to do just that.

RxJava's Observable chains seem a good match for the threads. It would be great if we could subscribe to our source and do all the transforming, combining, and filtering in the background and, when everything is done, have the result to be passed to the main threads. Yes, this sounds wonderful, but RxJava is single-threaded by default. This means that, in the most cases, when the subscribe method is called on an Observable instance, the current thread blocks until everything is emitted. (This is not true for the Observable instances created by the interval or timer factory methods, for example.). This is a good thing because working with threads is not so easy. They are powerful, but they need to be synchronized with...