Blocking subscribers
Remember how sometimes we have to stop the main thread from racing past an Observable
or Flowable
that operates on a different thread and keep it from exiting the application before it has a chance to fire? We often prevented this using Thread.sleep()
, especially when we used Observable.interval()
, subscribeOn()
, or observeOn()
. The following code shows how we did this typically and kept an Observable.interval()
application alive for five seconds:
import io.reactivex.Observable; import java.util.concurrent.TimeUnit; public class Launcher { public static void main(String[] args) { Observable.interval(1, TimeUnit.SECONDS) .take(5) .subscribe(System.out::println); sleep(5000); } public static void sleep(int millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { e.printStackTrace(); } } }
When it comes to unit testing, the unit...