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 6 seconds (just a second longer than the number of emitted intervals):
import io.reactivex.rxjava3.core.Observable;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
public class Ch10_01 {
@Test
public void demoCode() {
Observable.interval(1, TimeUnit.SECONDS)
.take(5)
.subscribe(System.out::println);
sleep(6000);
}
public static void sleep(int millis) {
...