Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean that an operator by default does not operate on any particular Scheduler

Tags:

rx-java

What does it mean that an operator by default does not operate on any particular Scheduler?

Example: CombineLatest.

RxJava implements this operator as combineLatest. It may take between two and nine Observables (as well as the combining function) as parameters, or a single List of Observables (as well as the combining function). It does not by default operate on any particular Scheduler.

like image 523
Marian Paździoch Avatar asked Apr 03 '18 14:04

Marian Paździoch


People also ask

What are schedulers in RxJava?

Android Scheduler — This Scheduler is provided by rxAndroid library. This is used to bring back the execution to the main thread so that UI modification can be made. This is usually used in observeOn method.

Which operator allows us to execute a function each time an item is emitted on the source Observable?

In the case of map operator, a project function is applied on each value on the source Observable and the same output is emitted as an Observable. A constant value is given as output along with the Observable every time the source Observable emits a value.

What is operator in RxJava?

This operator transforms each item emitted by an Observable but instead of returning the modified item, it returns the Observable itself which can emit data again. In other words, they merge items emitted by multiple Observables and returns a single Observable.

What is ObserveOn in RxJava?

ObserveOn specify the Scheduler on which an observer will observe this Observable. So basically SubscribeOn is mostly subscribed (executed) on a background thread ( you do not want to block the UI thread while waiting for the observable) and also in ObserveOn you want to observe the result on a main thread...


2 Answers

Operators do not require a particular Scheduler when they do not perform thread management operations.

That doesn't mean the code is not thread-safe. Thread safety is achieved by using scope containment, stack variables, volatile values and Atomic variables, with minimal use of synchronized clauses.

Thread management is more sophisticated, and means moving operations or data between threads. In the code for such operators, you will see them creating Runnables, or TimerTasks, which require knowing the scheduler.

like image 45
Bob Dalgleish Avatar answered Oct 23 '22 05:10

Bob Dalgleish


It means that the operator follows this guideline stated in the Scheduler docs --

By default, an Observable and the chain of operators that you apply to it will do its work, and will notify its observers, on the same thread on which its Subscribe method is called.

Everything it does will happen on the thread that subscribe was called on. Most operators work this way. Some cannot perform their work on same thread (like sample), so need a second thread to perform the work. These operators generally have a default Scheduler that they'll use for work, as well as a version that accepts a Scheduler as a parameter to let you choose where they get the second thread to work on.

like image 200
iagreen Avatar answered Oct 23 '22 03:10

iagreen