挂死示例:
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。