RxJava scheduler to watch the main thread

If I write something like this, then both the activity and notification are included in the current stream ...

Observable.fromCallable(() -> "Do Something")
    .subscribe(System.out::println);

      

If I perform an operation on a background thread like this, then both the operation and notification are on the background thread ...

Observable.fromCallable(() -> "Do Something")
    .subscribeOn(Schedulers.io())
    .subscribe(System.out::println);

      

If I want to watch in the main thread and do in the background in Android I would do ...

Observable.fromCallable(() -> "Do Something")
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(System.out::println);

      

But If I were writing a standard Java program, what is the equivalent of the state you want to observe on the main thread?

+10


source to share


2 answers


Convert Observable

to BlockingObservable

via .toBlocking()

; this gives you blocking methods to wait for completion, get one item, etc.



+5


source


For RxJava2 use " blockingSubscribe()

"



Flowable.fromArray(1, 2, 3)
                .subscribeOn(Schedulers.computation())
                .blockingSubscribe(integer -> {
                    System.out.println(Thread.currentThread().getName());
                });

      

+5


source







All Articles