# RxJava: Reactive Extensions for the JVM
<a href='https://github.com/ReactiveX/RxJava/actions?query=workflow%3ASnapshot'><img src='https://github.com/ReactiveX/RxJava/workflows/Snapshot/badge.svg'></a>
[![codecov.io](http://codecov.io/github/ReactiveX/RxJava/coverage.svg?branch=3.x)](https://codecov.io/gh/ReactiveX/RxJava/branch/3.x)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.reactivex.rxjava3/rxjava/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.reactivex.rxjava3/rxjava)
[![Contribute with Gitpod](https://img.shields.io/badge/Contribute%20with-Gitpod-908a85?logo=gitpod)](https://gitpod.io/#https://github.com/ReactiveX/RxJava)
[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/ReactiveX/RxJava/badge)](https://securityscorecards.dev/viewer/?uri=github.com/ReactiveX/RxJava)
RxJava is a Java VM implementation of [Reactive Extensions](http://reactivex.io): a library for composing asynchronous and event-based programs by using observable sequences.
It extends the [observer pattern](http://en.wikipedia.org/wiki/Observer_pattern) to support sequences of data/events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety and concurrent data structures.
#### Version 3.x ([Javadoc](http://reactivex.io/RxJava/3.x/javadoc/))
- Single dependency: [Reactive-Streams](https://github.com/reactive-streams/reactive-streams-jvm).
- Java 8+ or Android API 21+ required.
- Java 8 lambda-friendly API.
- [Android](https://github.com/ReactiveX/RxAndroid) desugar friendly.
- Fixed API mistakes and many limits of RxJava 2.
- Intended to be a replacement for RxJava 2 with relatively few binary incompatible changes.
- Non-opinionated about the source of concurrency (threads, pools, event loops, fibers, actors, etc.).
- Async or synchronous execution.
- Virtual time and schedulers for parameterized concurrency.
- Test and diagnostic support via test schedulers, test consumers and plugin hooks.
- Interop with newer JDK versions via 3rd party libraries, such as
- [Java 9 Flow API](https://github.com/akarnokd/RxJavaJdk9Interop#rxjavajdk9interop)
- [Java 21 Virtual Threads](https://github.com/akarnokd/RxJavaFiberInterop#rxjavafiberinterop)
Learn more about RxJava in general on the <a href="https://github.com/ReactiveX/RxJava/wiki">Wiki Home</a>.
:information_source: Please read the [What's different in 3.0](https://github.com/ReactiveX/RxJava/wiki/What's-different-in-3.0) for details on the changes and migration information when upgrading from 2.x.
#### Version 2.x
The [2.x version](https://github.com/ReactiveX/RxJava/tree/2.x) is end-of-life as of **February 28, 2021**. No further development, support, maintenance, PRs and updates will happen. The [Javadoc]([Javadoc](http://reactivex.io/RxJava/2.x/javadoc/)) of the very last version, **2.2.21**, will remain accessible.
#### Version 1.x
The [1.x version](https://github.com/ReactiveX/RxJava/tree/1.x) is end-of-life as of **March 31, 2018**. No further development, support, maintenance, PRs and updates will happen. The [Javadoc]([Javadoc](http://reactivex.io/RxJava/1.x/javadoc/)) of the very last version, **1.3.8**, will remain accessible.
## Getting started
### Setting up the dependency
The first step is to include RxJava 3 into your project, for example, as a Gradle compile dependency:
```groovy
implementation "io.reactivex.rxjava3:rxjava:3.x.y"
```
(Please replace `x` and `y` with the latest version numbers: [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.reactivex.rxjava3/rxjava/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.reactivex.rxjava3/rxjava)
)
### Hello World
The second is to write the **Hello World** program:
```java
package rxjava.examples;
import io.reactivex.rxjava3.core.*;
public class HelloWorld {
public static void main(String[] args) {
Flowable.just("Hello world").subscribe(System.out::println);
}
}
```
Note that RxJava 3 components now live under `io.reactivex.rxjava3` and the base classes and interfaces live under `io.reactivex.rxjava3.core`.
### Base classes
RxJava 3 features several base classes you can discover operators on:
- [`io.reactivex.rxjava3.core.Flowable`](http://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Flowable.html): 0..N flows, supporting Reactive-Streams and backpressure
- [`io.reactivex.rxjava3.core.Observable`](http://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Observable.html): 0..N flows, no backpressure,
- [`io.reactivex.rxjava3.core.Single`](http://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Single.html): a flow of exactly 1 item or an error,
- [`io.reactivex.rxjava3.core.Completable`](http://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Completable.html): a flow without items but only a completion or error signal,
- [`io.reactivex.rxjava3.core.Maybe`](http://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Maybe.html): a flow with no items, exactly one item or an error.
### Some terminology
#### Upstream, downstream
The dataflows in RxJava consist of a source, zero or more intermediate steps followed by a data consumer or combinator step (where the step is responsible to consume the dataflow by some means):
```java
source.operator1().operator2().operator3().subscribe(consumer);
source.flatMap(value -> source.operator1().operator2().operator3());
```
Here, if we imagine ourselves on `operator2`, looking to the left towards the source is called the **upstream**. Looking to the right towards the subscriber/consumer is called the **downstream**. This is often more apparent when each element is written on a separate line:
```java
source
.operator1()
.operator2()
.operator3()
.subscribe(consumer)
```
#### Objects in motion
In RxJava's documentation, **emission**, **emits**, **item**, **event**, **signal**, **data** and **message** are considered synonyms and represent the object traveling along the dataflow.
#### Backpressure
When the dataflow runs through asynchronous steps, each step may perform different things with different speed. To avoid overwhelming such steps, which usually would manifest itself as increased memory usage due to temporary buffering or the need for skipping/dropping data, so-called backpressure is applied, which is a form of flow control where the steps can express how many items are they ready to process. This allows constraining the memory usage of the dataflows in situations where there is generally no way for a step to know how many items the upstream will send to it.
In RxJava, the dedicated `Flowable` class is designated to support backpressure and `Observable` is dedicated to the non-backpressured operations (short sequences, GUI interactions, etc.). The other types, `Single`, `Maybe` and `Completable` don't support backpressure nor should they; there is always room to store one item temporarily.
#### Assembly time
The preparation of dataflows by applying various intermediate operators happens in the so-called **assembly time**:
```java
Flowable<Integer> flow = Flowable.range(1, 5)
.map(v -> v * v)
.filter(v -> v % 3 == 0)
;
```
At this point, the data is not flowing yet and no side-effects are happening.
#### Subscription time
This is a temporary state when `subscribe()` is called on a flow that establishes the chain of processing steps internally:
```java
flow.subscribe(System.out::println)
````
This is when the **subscription side-effects** are triggered (see `doOnSubscribe`). Some sources block or start emitting items right away in this state.
#### Runtime
This is the state when the flows are actively emitting items, errors or completion signals:
```java
Observable.create(emitter -> {
while (!emitter.isDisposed()) {
long time = System.currentTimeMillis();
emit
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
RxJava:响应式编程的现代方式 在当今的软件开发世界中,异步编程和事件驱动的架构变得越来越重要。RxJava,作为响应式编程(Reactive Programming)的一个流行库,为Java和Android开发者提供了一种强大的方式来处理异步任务和事件流。本文将深入探讨RxJava的核心概念、优势以及如何在实际项目中应用它。 RxJava是一个实现响应式编程的库,它提供了一套丰富的API来创建、组合和处理异步数据流。RxJava基于观察者模式,允许开发者以声明式的方式处理数据流,而不是传统的命令式编程。 响应式编程的优势 1. **异步处理**:响应式编程允许异步处理数据流,这意味着可以非阻塞地执行任务,从而提高应用程序的性能和响应性。 2. **代码可读性**:使用声明式编程,代码更加简洁和易于理解。 3. **可组合性**:响应式编程的流可以轻松组合和重用,使得代码更加模块化。 4. **错误处理**:RxJava提供了强大的错误处理机制,使得异常管理更加简单和集中。 5. **背压管理**:在数据生产速度超过消费速度时,RxJava可以帮助管理背压,防止系统过载。
资源推荐
资源详情
资源评论
收起资源包目录
【0积分下载】RxJava 3.x 最新版 (1960个子文件)
gradlew.bat 3KB
COPYRIGHT 569B
stylesheet.css 13KB
.gitattributes 366B
.gitignore 985B
build.gradle 6KB
javadoc_cleanup.gradle 5KB
settings.gradle 26B
gradlew 8KB
HEADER 570B
HEADER_JAVA 604B
gradle-wrapper.jar 60KB
Flowable.java 1.13MB
Observable.java 940KB
Maybe.java 322KB
Single.java 292KB
Completable.java 172KB
TestHelper.java 127KB
CompletableTest.java 116KB
FlowableGroupByTest.java 102KB
MaybeTest.java 85KB
ParallelFlowable.java 82KB
FlowableBufferTest.java 77KB
FlowableReplayEagerTruncateTest.java 70KB
FlowableReplayTest.java 68KB
ParamValidationCheckerTest.java 67KB
FlowableZipTest.java 66KB
FlowableObserveOnTest.java 65KB
ObservableReplayEagerTruncateTest.java 62KB
FlowableCombineLatestTest.java 61KB
RxJavaPluginsTest.java 60KB
ObservableGroupByTest.java 59KB
ObservableBufferTest.java 57KB
FlowableMergeTest.java 57KB
FlowableConcatTest.java 56KB
JavadocWording.java 54KB
ObservableReplayTest.java 53KB
FlowablePublishTest.java 52KB
TestSubscriberExTest.java 51KB
RxJavaPlugins.java 51KB
ReplayProcessorTest.java 50KB
ObservableSwitchTest.java 50KB
ObservableZipTest.java 48KB
TestSubscriberTest.java 48KB
FlowableSwitchTest.java 48KB
FlowableFlatMapTest.java 48KB
FlowableRetryTest.java 47KB
FlowableRefCountTest.java 45KB
ObservableCombineLatestTest.java 45KB
FlowableReplay.java 44KB
ReplaySubject.java 44KB
ReplayProcessor.java 44KB
ObservableRetryTest.java 44KB
FlowableNullTests.java 43KB
ObservableRefCountTest.java 43KB
FlowableConcatMapSchedulerTest.java 43KB
FlowableConcatMapEagerTest.java 42KB
SerializedSubscriberTest.java 42KB
ObservableFlatMapTest.java 41KB
FlowableWindowWithTimeTest.java 41KB
SerializedObserverTest.java 41KB
OperatorMatrixGenerator.java 41KB
ObservableConcatTest.java 40KB
ObservableMergeTest.java 40KB
ParallelFlowableTest.java 39KB
ObservableConcatMapSchedulerTest.java 38KB
ReplaySubjectTest.java 38KB
FlowableCreateTest.java 37KB
ObservableReplay.java 37KB
ObservableTest.java 37KB
TestObserverExTest.java 36KB
FlowableTests.java 36KB
ObservableNullTests.java 36KB
Schedulers.java 36KB
FlowableDelayTest.java 35KB
TestObserverTest.java 35KB
ObservableWindowWithTimeTest.java 35KB
FlowableFromIterableTest.java 34KB
ExecutorSchedulerInterruptibleTest.java 33KB
ObservableConcatMapEagerTest.java 33KB
ObservableDelayTest.java 33KB
FlowableFlattenIterableTest.java 32KB
Scheduler.java 31KB
FlowableTimeoutWithSelectorTest.java 31KB
XFlatMapTest.java 31KB
FlowableDoOnEachTest.java 29KB
FlowableBackpressureTests.java 29KB
ObservableTimeoutWithSelectorTest.java 29KB
BehaviorProcessorTest.java 28KB
ObservableObserveOnTest.java 27KB
FlowableMergeDelayErrorTest.java 27KB
AbstractSchedulerTests.java 26KB
FlowableGroupJoinTest.java 26KB
FlowableWithLatestFromTest.java 26KB
FlowableSingleTest.java 26KB
ObservableGroupJoinTest.java 26KB
SafeSubscriberTest.java 25KB
ObservablePublishTest.java 25KB
FlowableWindowTimed.java 25KB
BehaviorSubjectTest.java 24KB
共 1960 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
技术无疆
- 粉丝: 3810
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功