RxJava2 嵌套使用blockingGet()导致线程挂死问题

挂死示例:

class Test {
    public Test(int a, BigDecimal b) {
        this.a = a;
        this.b = b;
    }

    public int a;
    public BigDecimal b;
}

public static void main(String[] args) {
    List<Test> testList = new LinkedList<>();
    testList.add(new Test(1, new BigDecimal(10)));

    Map map = Observable.fromIterable(testList).groupBy(new Function<Test, Integer>() {
        @Override
        public Integer apply(Test test) throws Exception {
            System.out.println("AA ******** " + Thread.currentThread().getName());
            return test.a;
        }
    })/*.observeOn(Schedulers.newThread())*/
        .reduce(new HashMap(), new BiFunction<HashMap, GroupedObservable<Integer, Test>, HashMap>() {
        @Override
        public HashMap apply(HashMap map, GroupedObservable<Integer, Test> integerTestGroupedObservable) throws Exception {
            System.out.println("BB ******** " + Thread.currentThread().getName());

            Map<String, Object> subMap = new HashMap<>();
            int a = integerTestGroupedObservable.getKey();

            // 会在这一行挂死
            List<Test> list = integerTestGroupedObservable.reduce(new LinkedList<Test>(), new BiFunction<LinkedList<Test>, Test, LinkedList<Test>>() {
                @Override
                public LinkedList<Test> apply(LinkedList<Test> tests, Test test) throws Exception {
                    System.out.println("CC ******** " + Thread.currentThread().getName());
                    tests.add(test);
                    return tests;
                }
            }).blockingGet();

            BigDecimal sum = Observable.fromIterable(list).reduce(new BigDecimal(0), new BiFunction<BigDecimal, Test, BigDecimal>() {
                @Override
                public BigDecimal apply(BigDecimal bigDecimal, Test test) throws Exception {
                    System.out.println("DD ******** " + Thread.currentThread().getName());
                    return bigDecimal.add(test.b);
                }
            }).blockingGet();

            subMap.put("list", list);
            subMap.put("sum", sum);

            map.put(a, subMap);
            return map;
        }
    }).blockingGet();

    System.out.println(map);
}

运行时会挂死main线程,日志如下:

AA ******** main
BB ******** main

调试发现,由于内部嵌套的“blockingGet()”同样运行在“main”线程下,导致了线程挂死。

解决方式,使用observeOn(),放开注释掉的代码:.observeOn(Schedulers.newThread())。

observeOn():指定下游运算所在的线程,遇到observeOn()链中的任何位置,它将使用observeOn所指定的线程来操作的后续切换和数据流推送。

放开后日志如下:

AA ******** main
BB ******** RxNewThreadScheduler-1
CC ******** RxNewThreadScheduler-1
DD ******** RxNewThreadScheduler-1
{1={sum=10, list=[com.superz.RxJava2Test$Test@75e79c77]}}

内部嵌套的“blockingGet()”运行在线程:RxNewThreadScheduler-1。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值