RxJava错误处理与线程并发

2017-12-20  本文已影响162人  SMSM
错误处理、线程并发

错误处理

RxJava线程并发

https://www.jianshu.com/p/3dcf462dca08
借助just().flatmap(),flatmap能够开辟新流的特性,对每个子流做线程切换,全局搜索如下,看完整代码

Observable.just(Integer.valueOf(s)).observeOn(Schedulers.io()).map(new Function<Object, Integer>() {}

12-20 10:24:06.712 4958-4988/com.pitaya.findviewbyiddemo E/MainActivity: RxCachedThreadScheduler-2flatMap apply
12-20 10:24:06.717 4958-4988/com.pitaya.findviewbyiddemo E/MainActivity: RxCachedThreadScheduler-2flatMap apply
12-20 10:24:06.723 4958-4989/com.pitaya.findviewbyiddemo E/MainActivity: RxCachedThreadScheduler-4
12-20 10:24:06.750 4958-4988/com.pitaya.findviewbyiddemo E/MainActivity: RxCachedThreadScheduler-2flatMap apply
12-20 10:24:06.751 4958-4990/com.pitaya.findviewbyiddemo E/MainActivity: RxCachedThreadScheduler-3
12-20 10:24:06.751 4958-4988/com.pitaya.findviewbyiddemo E/MainActivity: RxCachedThreadScheduler-2flatMap apply
12-20 10:24:06.751 4958-4987/com.pitaya.findviewbyiddemo E/MainActivity: RxCachedThreadScheduler-1
12-20 10:24:06.759 4958-4991/com.pitaya.findviewbyiddemo E/MainActivity: RxCachedThreadScheduler-5
12-20 10:24:06.834 4958-4958/com.pitaya.findviewbyiddemo E/MainActivity: 我牛逼1  1
12-20 10:24:06.834 4958-4958/com.pitaya.findviewbyiddemo E/MainActivity: 1000292348执行成功了1001
12-20 10:24:06.835 4958-4958/com.pitaya.findviewbyiddemo E/MainActivity: 1000292348执行成功了922
12-20 10:24:06.835 4958-4958/com.pitaya.findviewbyiddemo E/MainActivity: 1000292348执行成功了1003
12-20 10:24:06.835 4958-4958/com.pitaya.findviewbyiddemo E/MainActivity: 1000292348执行成功了1002
12-20 10:24:06.836 4958-4958/com.pitaya.findviewbyiddemo E/MainActivity: 1000292348执行成功了33
12-20 10:24:06.836 4958-4958/com.pitaya.findviewbyiddemo E/MainActivity: 在 Consumer中抛出的异常  3
12-20 10:24:06.836 4958-4958/com.pitaya.findviewbyiddemo E/MainActivity: 1000292348执行成功了44
12-20 10:24:06.836 4958-4958/com.pitaya.findviewbyiddemo E/MainActivity: 1000292348执行成功了444
12-20 10:24:06.836 4958-4958/com.pitaya.findviewbyiddemo E/MainActivity: 1000292348执行成功了4444
12-20 10:24:06.844 4958-4958/com.pitaya.findviewbyiddemo E/MainActivity: 我牛逼5 5
12-20 10:24:11.761 4958-4958/com.pitaya.findviewbyiddemo D/EventBus: No subscribers registered for event class com.pitaya.findviewbyiddemo.MainActivity$MessageEvent
12-20 10:24:11.763 4958-4958/com.pitaya.findviewbyiddemo D/EventBus: No subscribers registered for event class com.pitaya.findviewbyiddemo.MainActivity$MessageEvent


 //TODO 错误的处理
       Observable.just("1", "2", "3").map(new Function<String, Integer>() {
           @Override
           public Integer apply(@NonNull String s) throws Exception {
               throw new NullPointerException("我牛逼1 ");
           }
       }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(integer -> {
           Log.e(TAG, MainActivity.this.hashCode() + "执行成功了" + integer);
           mNameActivityUtil.mEventBtn.setText("被修改了");
//            throw new NullPointerException("我牛逼 ");  .onErrorReturnItem(55)
       }, throwable -> {
           Log.e(TAG, throwable.getMessage() + " 1");
       });

       Observable.just("1", "2","22", "3").flatMap(new Function<String, ObservableSource<?>>() {
           @Override
           public ObservableSource<Integer> apply(@NonNull String s) throws Exception {
               Log.e(TAG, Thread.currentThread().getName() + "flatMap apply");
               return Observable.just(Integer.valueOf(s)).observeOn(Schedulers.io()).map(new Function<Object, Integer>() {
                   @Override
                   public Integer apply(@NonNull Object o) throws Exception {
                       Log.e(TAG, Thread.currentThread().getName());

                       if (22 == (Integer) o) {
                           throw new NullPointerException("我牛逼2 ");
                       }

                       return 1000 + (Integer) o;
                   }
               }).onErrorReturnItem(922);
           }
       }).onErrorResumeNext(new Function<Throwable, ObservableSource<? extends Integer>>() {
           @Override
           public ObservableSource<? extends Integer> apply(@NonNull Throwable throwable) throws Exception {
               return Observable.just(999);
           }
       }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(integer -> {
           Log.e(TAG, MainActivity.this.hashCode() + "执行成功了" + integer);
           mNameActivityUtil.mEventBtn.setText("被修改了");
       }, throwable -> {
           Log.e(TAG, throwable.getMessage() + " 2");
       });

       Observable.just("1", "2", "3").map(new Function<String, Integer>() {
           @Override
           public Integer apply(@NonNull String s) throws Exception {
               throw new NullPointerException("我牛逼3 ");
           }
       }).map(new Function<Integer, Integer>() {

           @Override
           public Integer apply(@NonNull Integer integer) throws Exception {
               return 3;
           }
       }).onErrorReturnItem(33).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(integer -> {
           Log.e(TAG, MainActivity.this.hashCode() + "执行成功了" + integer);
           mNameActivityUtil.mEventBtn.setText("被修改了");
           throw new NullPointerException("在 Consumer中抛出的异常 ");
       }, throwable -> {
           Log.e(TAG, throwable.getMessage() + " 3");
       });

       Observable.just("1").map(new Function<String, Integer>() {
           @Override
           public Integer apply(@NonNull String s) throws Exception {
               throw new NullPointerException("我牛逼4 ");
           }
       }).map(new Function<Integer, Integer>() {

           @Override
           public Integer apply(@NonNull Integer integer) throws Exception {
               return 666;
           }
       }).onErrorResumeNext(new Function<Throwable, ObservableSource<? extends Integer>>() {
           @Override
           public ObservableSource<? extends Integer> apply(@NonNull Throwable throwable) throws Exception {
               return Observable.just(44, 444, 4444);
           }
       }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(integer -> {
           Log.e(TAG, MainActivity.this.hashCode() + "执行成功了" + integer);
           mNameActivityUtil.mEventBtn.setText("被修改了");
       }, throwable -> {
           Log.e(TAG, throwable.getMessage() + " 4");
       });

       Observable.just("1", "2", "3").flatMap(new Function<String, ObservableSource<Integer>>() {
           @Override
           public ObservableSource<Integer> apply(@NonNull String s) throws Exception {
               return Observable.error(new NullPointerException("我牛逼5"));
           }
       }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(integer -> {
           Log.e(TAG, MainActivity.this.hashCode() + "执行成功了" + integer);
           mNameActivityUtil.mEventBtn.setText("被修改了");
       }, throwable -> {
           Log.e(TAG, throwable.getMessage() + " 5");
       });




上一篇 下一篇

猜你喜欢

热点阅读