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 theforEach()
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...