X Tutup
Skip to content

Commit 5051a45

Browse files
committed
Improved phrasing
1 parent ebcdae6 commit 5051a45

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

Part 4 - Concurrency/1. Scheduling and threading.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public final Observable<T> subscribeOn(Scheduler scheduler)
4646

4747
In Rx you don't juggle threads directly. Instead you wrap them in policies called `Scheduler`. We will see more on that later.
4848

49+
### subscribeOn
50+
4951
With `subscribeOn` you decide on what thread the `Observable.create` is executed. Even if you're not calling `create` yourself, there is an internal equivalent to it. Consider the following example:
5052

5153
```java
@@ -73,7 +75,7 @@ Received 2 on 1
7375
Finished main: 1
7476
```
7577

76-
We see here that, non only is everything executed on the same thread, it is actually sequential: `create` does not unblock until our lambda has completed, and that include the calls to `onNext`, which execute the entire chain of operators. Effectively, `create` is blocking.
78+
We see here that, non only is everything executed on the same thread, it is actually sequential: `subscribe` does not unblock until it has completed creating (and subscribing to) its observable, which means executing all of `create`'s lambda. The calls to `onNext` within that lambda execute the entire chain of operators, all the way to the `println`. Effectively, `create` is blocking.
7779

7880
If you uncomment `.subscribeOn(Schedulers.newThread())`, the output now is
7981
```
@@ -84,9 +86,9 @@ Received 1 on 11
8486
Received 2 on 11
8587
```
8688

87-
`Schedulers.newThread()` provided a new thread for our lambda function to run on. `create` no longer blocks and the main thread is free to proceed.
89+
`Schedulers.newThread()` provided a new thread for our lambda function to run on. `subscribe` no longer blocks until `create`'s lambda is executed and the main thread is free to proceed.
8890

89-
Some observables create their own threads regardless of you what you requested. For example, `Observable.interval` is anyway asynchronous. In such cases, `subscribeOn` will dictate on what thread to run the function which creates the resources. It gives you no control over what resources the source of your observable requires.
91+
Some observables create their own threads regardless of you what you requested. For example, `Observable.interval` is asynchronous regardless. In such cases, `subscribeOn` will dictate on what thread to run the function which creates the resources, which typically won't be helpful. It gives you no control over what resources the source of your observable requires.
9092

9193
```java
9294
System.out.println("Main: " + Thread.currentThread().getId());
@@ -107,18 +109,20 @@ Received 1 on 11
107109
Received 2 on 11
108110
```
109111

110-
`observeOn` controls the other side of the pipeline. The creation and emission of values will work like normal, but the actions of your observer will be invoked on a different thread, as specified by the `Scheduler`.
112+
### observeOn
113+
114+
`observeOn` controls the other side of the pipeline. The creation and emission of values will work like normal, but the actions of your observer will be invoked on a different thread, as specified by the `Scheduler` policy.
111115

112116
```java
113117
Observable.create(o -> {
114-
System.out.println("Created on " + Thread.currentThread().getId());
115-
o.onNext(1);
116-
o.onNext(2);
117-
o.onCompleted();
118-
})
119-
.observeOn(Schedulers.newThread())
120-
.subscribe(i ->
121-
System.out.println("Received " + i + " on " + Thread.currentThread().getId()));
118+
System.out.println("Created on " + Thread.currentThread().getId());
119+
o.onNext(1);
120+
o.onNext(2);
121+
o.onCompleted();
122+
})
123+
.observeOn(Schedulers.newThread())
124+
.subscribe(i ->
125+
System.out.println("Received " + i + " on " + Thread.currentThread().getId()));
122126
```
123127
Output
124128
```

0 commit comments

Comments
 (0)
X Tutup