However, what will happen when there are multiple sources? Let's examine this example:
Subject<Long> subject = PublishSubject.create();
Observable.interval(2, TimeUnit.SECONDS)
.take(3)
.doOnComplete(() -> log("Origin-One-doOnComplete"))
.subscribe(subject);
Observable.interval(1, TimeUnit.SECONDS)
.take(2)
.doOnComplete(() -> log("Origin-Two-doOnComplete"))
.subscribe(subject);
subject
.doOnComplete(() -> log("First-doOnComplete"))
.subscribe(v -> log(v));
Here, there are two Observables. The first is this:
Observable.interval(2, TimeUnit.SECONDS)
The second one is as following:
Observable.interval(1, TimeUnit.SECONDS)
They are emitting values to the Subject (or rather Subject listens to them), but we can see that the second Observable will complete before the first one.
Let's take a look at the output:
RxComputationThreadPool-2:0
RxComputationThreadPool-1:0
RxComputationThreadPool-2:1
RxComputationThreadPool-2:Origin-Two-doOnComplete
RxComputationThreadPool-2:First-doOnComplete
RxComputationThreadPool-1:Origin-One-doOnComplete
As soon as the second Observable completed (because it was faster and had to emit fewer items), the Subject completed as well and stopped receiving values from the first Observable. This means that Subject completes whenever one of the upstream Observables complete.
Also, if none of the upstream Observables complete, it means that the Subject will not complete as well.
This knowledge will come in handy when there will be multiple data sources for the Subjects you will be working with.