tutorial, no_image, rx,

Code - no_image

Upendra Upendra Follow Jan 23, 2025 · 2 mins read
Code - no_image
Share this

Puzzle 1 - subscribeOn + observeOn

On which thread doSomething() will be executed?

Observable.just("item")
    .observeOn(Schedulers.io())
    .subscribeOn(Schedulers.computation())
    .subscribe { doSomething() }

A) Main thread

B) Computation

C) IO

D) It’s not exectued

C) IO

Puzzle 2 - 2 subscribeOn

On which thread doSomething() will be executed?

Observable.just("item")
    .subscribeOn(Schedulers.computation())
    .subscribeOn(Schedulers.io())
    .subscribe { doSomething() }

A) Main thread

B) Computation

C) IO

D) It’s not exectued

B) Computation

Puzzle 3 - subscribeOn + 2 doOnSubscribe

On which order foo() and bar() will be executed?

Observable.just("item")
    .doOnSubscribe { foo() }
    .doOnSubscribe { bar() }
    .subscribeOn(Schedulers.computation())
    .subscribe()

A) foo() then bar()

B) bar() then foo()

C) only foo() is executed

D) It’s not executed

A) foo() then bar()

Puzzle 4 - 2 subscribeOn + 2 doOnSubscribe

On which order foo() and bar() will be executed?

Observable.just("item")
    .doOnSubscribe { foo() }
    .subscribeOn(Schedulers.computation())
    .doOnSubscribe { bar() }
    .subscribeOn(Schedulers.computation())
    .subscribe()

A) foo() then bar()

B) bar() then foo()

C) only foo() is executed

D) only bar() is executed

B) bar() then foo()

Puzzle 5 - 3 subscribeOn + 2 doOnSubscribe

On which thread foo() and bar() will be executed?

Observable.just("item")
    .subscribeOn(Schedulers.single())
    .doOnSubscribe { foo() }
    .subscribeOn(Schedulers.computation())
    .doOnSubscribe { bar() }
    .subscribeOn(Schedulers.io())
    .subscribe()

A) foo() on Single then bar() on Computation

B) bar() on IO then foo() on Computation

C) bar() on Computation then foo() on Single

D) I give up

B) bar() on IO then foo() on Computation

Puzzle 6 - subscribeOn + timer

On which thread doSomething() will be executed?

Observable.timer(10, TimeUnit.MILLISECONDS)
    .subscribeOn(Schedulers.io())
    .subscribe { doSomething() }

A) doSomething is executed on IO

B) doSomething is executed on Single

C) doSomething is executed on Computation

D) doSomething is executed on main

C) doSomething is executed on Computation

Puzzle 7 - subscribeOn + Subject

On which thread doSomething() will be executed?

val subject = BehaviorSubject.create<String>()

Observable.just(0)
    .observeOn(Schedulers.io())
    .subscribe { subject.onNext("any") }

Thread.sleep(10)

subject.subscribe { doSomething(it) }

A) doSomething is executed on main

B) doSomething is executed on Computation

C) doSomething is not executed

D) doSomething is executed on io

A) doSomething is executed on main

Links

RxJava2 puzzle: Do you understand subscribeOn?

Further reading

RxJava2 subscribeOn: How to use it

credit goes to @swayangjit
Join Newsletter
Get the latest news right in your inbox. We never spam!
Upendra
Written by Upendra Follow
Hi, I am Upendra, the author in Human and machine languages,I don't know to how 3 liner bio works so just Connect with me on social sites you will get to know me better.