-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSubscribeOn.java
87 lines (77 loc) · 3.28 KB
/
SubscribeOn.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.xuexiang.rxjava3sample.fragment.operators.utility;
import android.view.View;
import com.xuexiang.rxjava3sample.core.AbstractRxJavaFragment;
import com.xuexiang.rxjava3sample.utils.ExecutorUtils;
import com.xuexiang.xpage.annotation.Page;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.schedulers.Schedulers;
/**
* 指定Observable自身在哪个调度器上执行
*
* subscribeOn()改变调用它之前,直到observeOn调用代码的线程
* observeOn()改变调用它之后代码的线程
*
* <p>
* https://reactivex.io/documentation/operators/subscribeon.html
* <p>
* https://www.kancloud.cn/luponu/rxjava_zh/974502
* <p>
* https://reactivex.io/documentation/scheduler.html
*/
@Page(name = "subscribeOn\n指定Observable自身在哪个调度器上执行")
public class SubscribeOn extends AbstractRxJavaFragment {
@Override
protected String getInstruction() {
return "你可以使用subscribeOn操作符指定Observable在一个特定的调度器上运转。\n" +
"subscribeOn()改变调用它之前代码的线程。\n" +
"observeOn()改变调用它之后代码的线程";
}
@Override
protected void doOperation(View view) {
Observable<String> observable = Observable.create((ObservableOnSubscribe<String>)
emitter -> {
printNormalThreadInfo("subscribe");
emitter.onNext("this is data:");
emitter.onComplete();
})
.subscribeOn(ExecutorUtils.getScheduler("My-Scheduler-1"))
.subscribeOn(Schedulers.io())
.map(x -> {
printWarningThreadInfo("map-1");
return x + 1;
})
.subscribeOn(ExecutorUtils.getScheduler("My-Scheduler-2"))
.map(x -> {
printWarningThreadInfo("map-2");
return x + 2;
})
.subscribeOn(ExecutorUtils.getScheduler("My-Scheduler-3"))
.doOnEach(notification -> printNormalThreadInfo(notification.toString()))
.observeOn(AndroidSchedulers.mainThread());
doSubscribe(observable, integer -> printNormalThreadInfo("onNext:" + integer));
}
private void printWarningThreadInfo(String action) {
printWarning(action + ", ThreadInfo:" + Thread.currentThread().getName());
}
private void printNormalThreadInfo(String action) {
printNormal(action + ", ThreadInfo:" + Thread.currentThread().getName());
}
}