-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OperatorTake (and others) use of mutating non-volatile fields #1334
Comments
Because the contract of an Observable is that is does not receive concurrent invocations of any on* methods. Anything producing data to it must either naturally be single-threaded or serialize itself. This allows an a Observer/Subscriber to always write code that assumes single-threaded behavior. |
Thanks Ben I'm happy with it not receiving concurrent invocations but is
|
If multiple threads are invoking |
So in my example .serialize would be called between observe on and take?
|
No, you don't need it as each of those operators conforms to the Rx contract. There is only 1 thread in your code ever invoking |
You should never have to use |
I did see that when I tested that but didn't understand how the computation I can say that I have seen multiple threads hitting my custom operator
|
ah woops I see my misunderstanding with the Scheduler, it draws one thread from the pool of course. I should have rephrased the question to use a parallel: Observable.range(1,1000).parallel(...).take(500); |
Yes, with parallel it is expected to have multiple threads call onNext to take in your example. It is thread-safe in 'take' due to it serializing the output via 'merge' (https://github.com/Netflix/RxJava/blob/master/rxjava-core/src/main/java/rx/internal/operators/OperatorParallel.java#L69) which does synchronization/volatile that ensure visibility across threads. |
Great Ben, that will improve performance for my use case now that I can use
|
All queries answered, thanks |
I'd love some clarification on this issue. Given say this observable:
why is the count in OperatorTake not protected from access from multiple threads with the keyword volatile (or using an AtomicInteger)?
The text was updated successfully, but these errors were encountered: