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

The BlockingObservable class


Every Observable instance can be turned into a BlockingObservable instance with the toBlocking() method. The BlockingObservable instance has multiple methods that block the current thread, while everything is emitted by the source Observable instance until an OnCompleted or OnError notification is sent. If there is an OnError notification, an exception will be thrown (RuntimeException exceptions are thrown directly and checked exceptions are wrapped inside the RuntimeException instances).

The toBlocking() method doesn't block by itself, but the methods of the BlockingObservable instance it returns may block. Let's look at some of those methods:

  • We can iterate over all the items in the BlockingObservable instance, using the forEach() method. Here is an example of using this:

    Observable
      .interval(100L, TimeUnit.MILLISECONDS)
      .take(5)
      .toBlocking()
      .forEach(System.out::println);
    System.out.println("END");

This is also an example of how to make asynchronous code...