Earlier in this book, we used Observable.create() to create our own Observable from scratch, which describes how to emit items when it is subscribed to, as shown in the following code snippet:
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.schedulers.Schedulers;
public class Ch8_09 {
public static void main(String[] args) {
Observable<Integer> source = Observable.create(emitter -> {
for (int i = 0; i <= 1000; i++) {
if (emitter.isDisposed()) {
return;
}
emitter.onNext(i);
}
emitter.onComplete();
});
source.observeOn(Schedulers.io())
.subscribe(System.out::println);
sleep(1000);
}
}
The output is as follows:
0
1
2
3
4
...
This Observable.create() emits the integers from 0 through 1000...