Java RxJava學習使用

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

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

  2. 創建一個Client.java類,實現main方法。接下來開始使用RxJava。

  3. 創建一個simple方法,該方法簡單的使用RxJava.

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

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

輸出為:

one
  1. 不同線程的調度,切換線程,不同線程中傳遞數據。
    /**
     * 線程示例
     * @throws InterruptedException 
     */
    public static void threadSimple() throws InterruptedException {
        Flowable//流
        .fromCallable(new Callable<String>() {//子線程調用
            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);
    }

打印結果:

RxCachedThreadScheduler-1
RxSingleScheduler-1
true

3.實現1-10數字的自乘。

    /**
     * map使用
     */
    public static void mapSimple() {
        Flowable//流
        .range(1, 10)//從1到10 
        .observeOn(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);
            }
        });
    }

打印結果:

1
4
9
16
25
36
49
64
81
100
  1. 實現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);
            }
        });
    }

打印結果:

9
16
25
36
49
64
1
4
81
100
  1. 從2.0.5開始,有一個“實驗”操作符的并行和“平行流”,有助于實現相同的并行處理模式。
    /**
     * 平行調用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);
            }
        });
    }

打印結果:

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

推薦閱讀更多精彩內容

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