在RxJava中线程安全是否必需SerializedSubject [英] Is SerializedSubject necessary for thread-safety in RxJava

查看:174
本文介绍了在RxJava中线程安全是否必需SerializedSubject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在RxJava中创建了一个 Subject 实例,并从多个线程调用它的 onNext()

I created a Subject instance in RxJava and call its onNext() from multiple threads:

PublishSubject<String> subject = PublishSubject.create();
//...
subject.onNext("A");  //thread A
subject.onNext("B");  //thread B

RxJava文档说:


小心不要调用它的<$来自多个线程的c $ c> onNext()方法(或其它方法),因为这可能导致非序列化调用,这违反了Observable合约并在结果<$中产生歧义c $ c>主题。

take care not to call its onNext( ) method (or its other on methods) from multiple threads, as this could lead to non-serialized calls, which violates the Observable contract and creates an ambiguity in the resulting Subject.




  • 我是否必须致电 toSerialized() on this Subject 假设我不在乎AB之前或之后去?序列化将如何帮助?

  • 无论如何主题线程安全还是我会在没有 toSerialized()的情况下中断RxJava

  • 文档中提到的 Observable contract 是什么?

    • Do I have to call toSerialized() on such Subject assuming I don't care if "A" goes before or after "B"? How would serialization help?
    • Is Subject thread-safe anyway or will I break RxJava without toSerialized()?
    • What is the "Observable contract" that the documentation mentions?
    • 推荐答案


      我必须在这样的主题上调用toSerialized(),假设我不在乎A是什么在B之前或之后?

      Do I have to call toSerialized() on such Subject assuming I don't care if "A" goes before or after "B"?

      是否使用 toSerialized()因为应用于主题的所有运算符都假设正确的序列化正在上游发生。如果没有发生这种情况,流可能会失败或产生意外结果。

      Yep use toSerialized() because all operators applied to the subject assume that proper serialization is occurring upstream. The stream may fail or produce unexpected results if this does not happen.


      无论如何都是主题线程安全的,否则我会在没有toSerialized的情况下中断RxJava( )?

      Is Subject thread-safe anyway or will I break RxJava without toSerialized()?

      上面的回答


      什么是文件中提到的可观察合同吗?

      What is the "Observable contract" that the documentation mentions?

      Rx Design Guidelines.pdf 第4节定义了Observable合同。我认为RxJava文档应该使这个更容易被发现,所以我会提出一个问题。

      Rx Design Guidelines.pdf section 4 defines the Observable contract. I think RxJava documentation should make this more discoverable so I'll raise an issue.

      这篇关于在RxJava中线程安全是否必需SerializedSubject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆