Java RxJava學(xué)習(xí)使用

本文使用eclipse編輯器,gradle依賴jar,如若未配置此環(huán)境,請轉(zhuǎn)Java Eclipse配置gradle編譯項目配置好環(huán)境后再查看此文

  1. 創(chuàng)建Gradle(STS) Project工程,并刪除其他一些不需要的文件。
截圖1.png
  1. 在build.gradle文件的dependencies中依賴,并刷新依賴。
    compile "io.reactivex.rxjava2:rxjava:2.1.3"

  2. 創(chuàng)建一個Client.java類,實現(xiàn)main方法。接下來開始使用RxJava。

  3. 創(chuàng)建一個simple方法,該方法簡單的使用RxJava.

    /**
     * 簡單使用
     */
    public static void simple() {
        Flowable//流
        .just("one") //數(shù)據(jù)
        .subscribe(new Consumer<String>() {//訂閱一個消費者

            public void accept(String t) throws Exception {
                System.out.println(t); // 打印數(shù)據(jù)              
            }
        });
    }

輸出為:

one
  1. 不同線程的調(diào)度,切換線程,不同線程中傳遞數(shù)據(jù)。
    /**
     * 線程示例
     * @throws InterruptedException 
     */
    public static void threadSimple() throws InterruptedException {
        Flowable//流
        .fromCallable(new Callable<String>() {//子線程調(diào)用
            public String call() throws Exception {
                System.out.println(Thread.currentThread().getName());
                Thread.sleep(1000);
                return "true";
            }
        })
        .subscribeOn(Schedulers.io())//io線程
        .observeOn(Schedulers.single())//單線程
        .subscribe(new Consumer<String>() {//主線程訂閱

            public void accept(String t) throws Exception {
                System.out.println(Thread.currentThread().getName());
                System.out.println(t);
            }
        }, new Consumer<Throwable>() {

            public void accept(Throwable t) throws Exception {
                System.out.println(t);
            }
        });
        Thread.sleep(2000);
    }

打印結(jié)果:

RxCachedThreadScheduler-1
RxSingleScheduler-1
true

3.實現(xiàn)1-10數(shù)字的自乘。

    /**
     * map使用
     */
    public static void mapSimple() {
        Flowable//流
        .range(1, 10)//從1到10 
        .observeOn(Schedulers.computation())//用于計算工作的實例     
        .map(new Function<Integer, Integer>() {//對每一項進(jìn)行自乘
            public Integer apply(Integer t) throws Exception {
                return t*t;
            }
        })
        .blockingSubscribe(new Consumer<Integer>() {//當(dāng)前線程回調(diào)

            public void accept(Integer t) throws Exception {
                System.out.println(t);
            }
        });
    }

打印結(jié)果:

1
4
9
16
25
36
49
64
81
100
  1. 實現(xiàn)1-10自乘,亂序打印。
    /**
     * flatMap使用
     */
    public static void flatMapSimple() {
        Flowable
        .range(1, 10)
        .flatMap(new Function<Integer, Publisher<? extends Integer>>() {

            public Publisher<? extends Integer> apply(Integer t) throws Exception {
                return Flowable
                        .just(t)
                        .subscribeOn(Schedulers.computation())
                        .map(new Function<Integer, Integer>() {

                            public Integer apply(Integer t) throws Exception {
                                return t*t;
                            }
                        });
            }
        })
        .blockingSubscribe(new Consumer<Integer>() {

            public void accept(Integer t) throws Exception {
                System.out.println(t);
            }
        });
    }

打印結(jié)果:

9
16
25
36
49
64
1
4
81
100
  1. 從2.0.5開始,有一個“實驗”操作符的并行和“平行流”,有助于實現(xiàn)相同的并行處理模式。
    /**
     * 平行調(diào)用map
     */
    public static void parallelSimple() {
        Flowable
        .range(1, 10)
        .parallel()//平行
        .runOn(Schedulers.computation())
        .map(new Function<Integer, Integer>() {

            public Integer apply(Integer t) throws Exception {
                return t*t;
            }
        })
        .sequential()//順序
        .blockingSubscribe(new Consumer<Integer>() {

            public void accept(Integer t) throws Exception {
                System.out.println(t);
            }
        });
    }

打印結(jié)果:

1
4
9
16
25
36
49
64
81
100
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,573評論 25 708
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,993評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,973評論 6 342
  • 風(fēng)說:做我女朋友吧,好不好。林小姐打開微信嚇了一跳,這家伙是不是瘋啦。"你是不是喝多了,說話口齒不清了,最近又遇到...
    林風(fēng)一諾閱讀 361評論 1 0
  • 【西游殤目錄】歡迎戳進(jìn)來 【上一章】西游殤(85)再無齊天 前情摘要: “幽冥中去吧!”眾人的攻勢一齊擊在悟空身上...
    傅人閱讀 3,910評論 70 71