RxJava是什么?

原文是这样描述的:

RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences.

翻译过来就是一个在 Java VM 上使用可观测的序列来组成异步的、基于事件的程序的库。

它是观察者模式的一个变种,既然是基于事件的,当然EventBus的东东它都可以实现。

下面就来说说几种常用的Subject(既是观察者又是被观察对象)

从下图可以看出它的子类有PublishSubject、BehaviorSubject、ReplaySubject、AsyncSubject、SerializedSubject。

PublishSubject

Subject that, once an Observer has subscribed, emits all subsequently observed items to the subscriber.

PublishSubject算是RxJava中最常用的Subject,一旦一个观察者订阅了该Subject,它会发送所有数据给订阅者。

什么意思?看图就明白了。

也就是说订阅者只会接受订阅之后的来自PublishSubject发射的数据。

Example Code

PublishSubject subject = PublishSubject.create();

// observer1 will receive all onNext and onCompleted events

subject.subscribe(observer1);

subject.onNext("one");

subject.onNext("two");

// observer2 will only receive "three" and onCompleted

subject.subscribe(observer2);

subject.onNext("three");

subject.onCompleted();

那么有人就会疑问?有没有一种Subject能接受订阅之前的数据呢?答案是有且不止一个,个人感觉使用场景最多的是BehaviorSubject

BehaviorSubject

官方是这样描述:

Subject that emits the most recent item it has observed and all subsequent observed items to each subscribed Observer.

大意是BehaviorSubject会发送离订阅最近的上一个值,没有上一个值的时候会发送默认值(如果有的话)

Example Code

// observer will receive all events.

BehaviorSubject subject = BehaviorSubject.create("default");

subject.subscribe(observer);

subject.onNext("one");

subject.onNext("two");

subject.onNext("three");

// observer will receive the "one", "two" and "three" events, but not "zero"

BehaviorSubject subject = BehaviorSubject.create("default");

subject.onNext("zero");

subject.onNext("one");

subject.subscribe(observer);

subject.onNext("two");

subject.onNext("three");

// observer will receive only onCompleted

BehaviorSubject subject = BehaviorSubject.create("default");

subject.onNext("zero");

subject.onNext("one");

subject.onCompleted();

subject.subscribe(observer);

// observer will receive only onError

BehaviorSubject subject = BehaviorSubject.create("default");

subject.onNext("zero");

subject.onNext("one");

subject.onError(new RuntimeException("error"));

subject.subscribe(observer);

ReplaySubject

Subject that buffers all items it observes and replays them to any Observer that subscribes.

该Subject会缓存所有的发射数据,无论观察者何时订阅,Subject都会将所有内容发送给订阅者。

Example Code

ReplaySubject subject = ReplaySubject.create();

subject.onNext("one");

subject.onNext("two");

subject.onNext("three");

subject.onCompleted();

// both of the following will get the onNext/onCompleted calls from above

subject.subscribe(observer1);

subject.subscribe(observer2);

AsyncSubject

Subject that publishes only the last item observed to each Observer once the source Observable has completed. The item is cached and published to any Observers which subscribe after the source has completed. If the source emitted no items, AsyncSubject completes without emitting anything. If the source terminated in an error, current and future subscribers will receive only the error.

概括的讲就是使用AsyncSubject无论发送多少个数据事件,观察者永远只能接受到最后一个数据(完成事件必须调用)。如果发送数据过程中出现错误,观察者仅仅接受到错误信息。

Example Code

// observer will receive no onNext events because the subject.onCompleted() isn't called.

AsyncSubject subject = AsyncSubject.create();

subject.subscribe(observer);

subject.onNext("one");

subject.onNext("two");

subject.onNext("three");

// observer will receive "three" as the only onNext event.

AsyncSubject subject = AsyncSubject.create();

subject.subscribe(observer);

subject.onNext("one");

subject.onNext("two");

subject.onNext("three");

subject.onCompleted();

SerializedSubject

Wraps a Subject so that it is safe to call its various on methods from different threads.

如果你把 Subject 当作一个 Subscriber 使用,千万要注意不要从多个线程中调用它的onNext(T)方法(包括其它的on系列方法),这可能导致同时非序列调用,这会违反Observable协议,给Subject的结果增加了不确定性。

要避免此类问题,你可以将 Subject 转换为一个 SerializedSubject

Example Code

mySafeSubject = new SerializedSubject( myUnsafeSubject );

java中subject类_RxJava中常见的几种Subject相关推荐

  1. java subject类_RxJava 常见误区(一):过度使用 Subject

    准备写这篇文章的时候看了下 RxJava 在 Github 上已经 12000+ 个 star 了,可见火爆程度,自己使用 RxJava 也已经有一小段时间.最初是在社区对 RxJava 一片赞扬之声 ...

  2. RxJava中常见的几种Subject

    RxJava是什么? 原文是这样描述的: RxJava is a Java VM implementation of Reactive Extensions: a library for compos ...

  3. java 在一个类中定义类_Java 中程序代码必须在一个类中定义,类使用( )关键字来定义。_学小易找答案...

    [填空题]每一个 else 子句都必须和它前面的一个距离它最近的 子句相对应. [单选题]有关类的说法,正确的是( ) [单选题]在JAVA中,无论测试条件是什么,下列( )循环体 将至少执行一次. ...

  4. java 取对象的类_Java中通过Class类获取Class对象的方法详解

    前言 本文主要给大家介绍的是关于Java通过Class类获取Class对象的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍: 阅读API的Class类得知,Class 没有公共 ...

  5. java中 如果类c_Java中,如果类C是类B的子类,类B是类A的子类,那么下面描述正确的是( )。_学小易找答案...

    [单选题]社会意识是() [判断题]刘易斯认为,在发展中国家,经济发展应当追求最高精尖的技术.( ) [单选题]财务杠杆反映的是 [判断题]是否承认社会历史的规律性,是唯物史观和唯心史观的根本对立. ...

  6. java 实现 string类_java 中String类的常用方法总结,带你玩转String类。

    String类: String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.String类对象创建后不能修改,StringBuffer & St ...

  7. java怎么创建日期类_java中的日期类Date

    一.Java中的日期概述 日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中,日期的国际化,日期和时间之间的转换,日期的加减运算,日期的展示格式都是非常复杂的问题. 在Java中 ...

  8. c语言面向对象编程中的类_C ++中的面向对象编程

    c语言面向对象编程中的类 Object oriented programming, OOP for short, aims to implement real world entities like ...

  9. python 类中定义类_Python中的动态类定义

    python 类中定义类 Here's a neat Python trick you might just find useful one day. Let's look at how you ca ...

  10. C语言面向对象编程的类是指,c语言面向对象编程中的类_C ++中的面向对象编程...

    c语言面向对象编程中的类 Object Oriented programming is a programming style that is associated with the concept ...

最新文章

  1. 测试 csdnmakerdown语法
  2. Flink从入门到精通100篇(四)-基于 Flink 和 Drools 的实时日志处理
  3. Core 定时任务之HangFire
  4. 理解伪元素 :before 和 :after
  5. 【转载】ABAP 字符串 ‘‘ || `` 区别
  6. CentOS系统启动流程及GRUB文件说明
  7. mysql 存储二进制数据_为什么在MySQL中存储二进制数据?
  8. 《Windows PowerShell实战指南(第2版)》——1.4 搭建自己的实验环境
  9. Spring boot 项目目录结构
  10. Android中Menu的基本用法
  11. C# 如何理解 ASP.NET Web API 的 REST
  12. 新微擎 v1.7.9 图文回复 标题emoji
  13. 通过数据,从键盘录入学生考试科目数,然后依次录入学的每一科分数.使用数组存储学生分数.然后输出总分,平均分,简单易理解
  14. 什么是软件测试?软件测试的目的、意义是什么? 软件测试的流程是什么?
  15. python指数函数ks检验_python指数函数不正确的指数值
  16. centos7 默认中文字体_centos7安装中文宋体
  17. 内置FET的小型表贴封装 ACDC转换器IC
  18. 二叉树的遍历(二叉树与递归算法)
  19. html——标签分类
  20. 深圳云计算培训:怎么进入云计算这个行业?

热门文章

  1. 配置CKFinder(Java版)
  2. 交换机和路由器技术-07-静态路由配置
  3. 计算机单片机考试作弊检讨书,考试作弊检讨书2000字5篇
  4. 【警告】扣扣热键你了解多少
  5. pytroch、tensorflow对比学习—功能组件(数据管道、回调函数、特征列处理)
  6. 哪些行业网站适合使用CDN加速?
  7. vue单页面SEO优化
  8. 2020年10种最佳移动开发编程语言
  9. POJ-2524 Ubiquitous Religions
  10. Shell 获取本月最后一天