You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+133-1Lines changed: 133 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,15 @@ RxJava is a Java VM implementation of [Reactive Extensions](http://reactivex.io)
8
8
9
9
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.
10
10
11
+
#### Version 1.x
12
+
13
+
Looking for version 1.x? Jump [to the 1.x branch](https://github.com/ReactiveX/RxJava/tree/1.x).
14
+
15
+
Timeline plans for the 1.x line:
16
+
17
+
-**June 1, 2017** - feature freeze (no new operators), only bugfixes
18
+
-**March 31, 2018** - end of life, no further development
19
+
11
20
#### Version 2.x
12
21
13
22
- single dependency: [Reactive-Streams](https://github.com/reactive-streams/reactive-streams-jvm)
@@ -23,6 +32,129 @@ Version 2.x and 1.x will live side-by-side for several years. They will have dif
23
32
24
33
See the differences between version 1.x and 2.x in the wiki article [What's different in 2.0](https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0). Learn more about RxJava in general on the <ahref="https://github.com/ReactiveX/RxJava/wiki">Wiki Home</a>.
25
34
35
+
## Getting started
36
+
37
+
The first step is to include RxJava 2 into your project, for example, as a Gradle compile dependency:
38
+
39
+
```groovy
40
+
compile "io.reactivex.rxjava2:rxjava:2.x.y"
41
+
```
42
+
43
+
The second is to write the **Hello World** program:
If your platform doesn't support Java 8 lambdas (yet), you have to create an inner class of `Consumer` manually:
58
+
59
+
```java
60
+
Flowable.just("Hello world")
61
+
.subscribe(newConsumer<String>() {
62
+
@Overridepublicvoidaccept(Strings) {
63
+
System.out.println(s);
64
+
}
65
+
);
66
+
```
67
+
68
+
RxJava2 features several base classes you can discover operators on:
69
+
70
+
- `io.reactivex.Flowable` :0..N flows, supporting Reactive-Streams and backpressure
71
+
- `io.reactivex.Observable`:0..N flows, no backpressure
72
+
- `io.reactivex.Single`: a flow of exactly 1 item or an error
73
+
- `io.reactivex.Completable`: a flow without items but only a completion or error signal
74
+
- `io.reactivex.Maybe`: a flow with no items, exactly one item or an error
75
+
76
+
One of the common use cases forRxJava is to run some computation, network request on a background thread and show the results (or error) on the UI thread:
Thread.sleep(2000); // <--- wait for the flow to finish
88
+
```
89
+
90
+
This style of chaining methods is called a **fluent API** which resembles the **builder pattern**.However, RxJava's reactive types are immutable; each of the method calls returns a new `Flowable` with added behavior. To illustrate, the example can be rewritten as follows:
Typically, you can move computations or blocking IO to some other thread via `subscribeOn`. Once the data is ready, you can make sure they get processed on the foreground or GUI thread via `observeOn`.
108
+
109
+
RxJava operators don't work `Thread`s or `ExecutorService`s directly but with so called `Scheduler`s that abstract away sources of concurrency behind an uniform API. RxJava 2 features several standard schedulers accessible via `Schedulers` utility class. These are available on all JVM platforms but some specific platforms, such as Android, have their own typical `Scheduler`s defined: `AndroidSchedulers.mainThread()`, `SwingScheduler.instance()` or `JavaFXSchedulers.gui()`.
110
+
111
+
The `Thread.sleep(2000);` at the end is no accident. InRxJava the default `Scheduler`s run on daemon threads, which means once the Java main thread exits, they all get stopped and background computations may never happen. Sleepingfor some time in this example situations let's you see the output of the flow on the console with time to spare.
112
+
113
+
Flows in RxJava are sequential in nature split into processing stages that may run **concurrently** with each other:
114
+
115
+
```java
116
+
Flowable.range(1, 10)
117
+
.observeOn(Schedulers.computation())
118
+
.map(v -> v * v)
119
+
.blockingSubscribe(System.out::println);
120
+
```
121
+
122
+
This example flow squares the numbers from 1 to 10 on the **computation** `Scheduler` and consumes the results on the "main" thread (more precisely, the caller thread of `blockingSubscribe`). However, the lambda `v -> v * v` doesn't run in parallel forthis flow; it receives the values 1 to 10 on the same computation thread one after the other.
123
+
124
+
Processing the numbers 1 to 10 in parallel is a bit more involved:
125
+
126
+
```java
127
+
Flowable.range(1, 10)
128
+
.flatMap(v ->
129
+
Flowable.just(v)
130
+
.subscribeOn(Schedulers.computation())
131
+
.map(v -> v * v)
132
+
)
133
+
.blockingSubscribe(System.out::println);
134
+
```
135
+
136
+
Practically, paralellism in RxJava means running independent flows and merging their results back into a single flow. The operator `flatMap` does this by first mapping each number from 1 to 10 into its own individual `Flowable`, runs them and merges the computed squares.
137
+
138
+
`flatMap` is a powerful operator and helps in a lot of situations. For example, given a service that returns a `Flowable`, we'd like to call another service with values emitted by the first service:
Note, however, that `flatMap` doesn't guarantee any order and the end result from the inner flows may end up interleaved. There are alternative operators:
152
+
153
+
- `concatMap` that maps and runs one inner flow at a time and
154
+
- `concatMapEager` which runs all inner flows "at once" but the output flow will be in the order those inner flows were created.
155
+
156
+
For further details, consult the [wiki](https://github.com/ReactiveX/RxJava/wiki).
157
+
26
158
## Communication
27
159
28
160
- Google Group: [RxJava](http://groups.google.com/d/forum/rxjava)
@@ -130,4 +262,4 @@ See the License for the specific language governing permissions and
0 commit comments