Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Reactive puzzlers (working title) #22

Open
akarnokd opened this issue May 2, 2016 · 2 comments
Open

Reactive puzzlers (working title) #22

akarnokd opened this issue May 2, 2016 · 2 comments

Comments

@akarnokd
Copy link
Contributor

akarnokd commented May 2, 2016

This issue, similar to #21, collects some interesting cases and pitfalls in the form of Java Puzzlers, giving a scenario and offering 4-6 answers.

@akarnokd
Copy link
Contributor Author

akarnokd commented May 2, 2016

  1. What happens if you run the following code?
Observable.empty().repeat().subscribe();

[ ] Nothing, completes normally
[ ] Throws MissingBackpressureException
[ ] Throws OnErrorNotImplementedException
[X] Goes into an infinite loop

The operator repeat resubscribes to the source when it completes. In this case, the empty() completes immediately, gets resubscribed and completes immediately again, ad infinitum.

Note that the same thing can happen with terminated subjects such as PublishSubject and BehaviorSubject or an empty ReplaySubject!

Takeaway: never repeat a source indefinitely, especially from an unknown source.

@akarnokd
Copy link
Contributor Author

akarnokd commented May 2, 2016

  1. What happens when you run the following code?
PublishSubject<Integer> ps = PublishSubject.create();

ps.subscribeOn(Schedulers.computation()).subscribe(v -> System.out.println(Thread.currentThread.getName()));

Thread.sleep(1000);

ps.onNext(1);

[X] Prints main
[ ] Prints 1
[ ] Prints RxComputationScheduler-1
[ ] Throws an Exception

The operator subscribeOn has no effect on a running PublishSubject because it doesn't have any visible subscription side-effects. However the other subjects may signal a value or completion on the given Scheduler.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

1 participant