1+ /*
2+ * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ *
16+ */
17+
18+ package com .xuexiang .rxjava3sample .fragment .schedulers ;
19+
20+ import android .view .View ;
21+
22+ import com .xuexiang .rxjava3sample .core .AbstractRxJavaFragment ;
23+ import com .xuexiang .xpage .annotation .Page ;
24+
25+ import io .reactivex .rxjava3 .android .schedulers .AndroidSchedulers ;
26+ import io .reactivex .rxjava3 .core .Observable ;
27+ import io .reactivex .rxjava3 .schedulers .Schedulers ;
28+
29+ /**
30+ * Schedulers共提供了如下5种线程调度器:
31+ * <p>
32+ * io:CachedThreadPool。这个调度器用于I/O操作,比如:读写文件,数据库,网络交互等等。行为模式和newThread()差不多,重点需要注意的是线程池是无限制的,大量的I/O调度操作将创建许多个线程并占用内存。
33+ * computation:FixedThreadPool。计算工作默认的调度器,这个计算指的是 CPU 密集型计算,即不会被 I/O 等操作限制性能的操作,例如图形的计算。这个 Scheduler 使用的固定的线程池,大小为 CPU 核数。
34+ * single:拥有一个线程单例,所有的任务都在这一个线程中执行,当此线程中有任务在执行的时候其他任务将按照队列先进先出的顺序依次执行。
35+ * newThread:为指定任务启动一个新的线程。
36+ * trampoline:在当前线程执行任务,如果当前线程有任务在执行,则会将其暂停下来,等插入进来的任务执行完成之后,再将未完成的任务接着执行。
37+ * <p>具体参见:
38+ * https://www.kancloud.cn/luponu/rxjava_zh/974451
39+ */
40+ @ Page (name = "Scheduler类型\n 默认提供5种线程调度器" )
41+ public class SchedulerType extends AbstractRxJavaFragment {
42+
43+ @ Override
44+ protected String getInstruction () {
45+ return "* io: 缓存线程池,线程数量无穷大,用于I/O操作。\n " +
46+ "* computation: 固定线程池,大小为CPU核数,用于CPU密集型计算(无阻塞)。\n " +
47+ "* single: 单线程池。\n " +
48+ "* newThread: 为指定任务启动一个新的线程。\n " +
49+ "* trampoline: 当其它排队的任务完成后,在当前线程排队开始执行。\n " ;
50+ }
51+
52+ @ Override
53+ protected void doOperation (View view ) {
54+ Observable <Integer > observable = Observable .just (1 )
55+ .subscribeOn (Schedulers .io ())
56+ .map (x -> {
57+ printWarningThreadInfo ("map-1" );
58+ return x + 1 ;
59+ })
60+ .observeOn (Schedulers .computation ())
61+ .map (x -> {
62+ printWarningThreadInfo ("map-2" );
63+ return x + 2 ;
64+ })
65+ .observeOn (Schedulers .newThread ())
66+ .map (x -> {
67+ printWarningThreadInfo ("map-3" );
68+ return x + 3 ;
69+ })
70+ .observeOn (Schedulers .single ())
71+ .map (x -> {
72+ printWarningThreadInfo ("map-4" );
73+ return x + 4 ;
74+ })
75+ .observeOn (Schedulers .trampoline ())
76+ .map (x -> {
77+ printWarningThreadInfo ("map-5" );
78+ return x + 5 ;
79+ })
80+ .observeOn (AndroidSchedulers .mainThread ());
81+
82+ doSubscribe (observable , integer -> printNormalThreadInfo ("onNext:" + integer ));
83+ }
84+
85+ private void printWarningThreadInfo (String action ) {
86+ printWarning (action + ", ThreadInfo:" + Thread .currentThread ().getName ());
87+ }
88+
89+ private void printNormalThreadInfo (String action ) {
90+ printNormal (action + ", ThreadInfo:" + Thread .currentThread ().getName ());
91+ }
92+
93+ }
0 commit comments