Closed
Description
In Android application I'm trying to perform a simple parallel task using ParallelFlowable
from RxJava 2.0.6.
public void doParallelAsyncJob() {
subscription = Flowable
.fromArray("1", "2", "3", "4", "5", "6", "7", "8")
.parallel()
.runOn(Schedulers.computation())
.map(Integer::parseInt)
.sequential()
.toList()
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
result -> showSuccessDialog(),
error -> showErrorDialog()
);
}
As you can see, the code takes several strings and tries to parse them as integers. In this case all numbers are processed successfully, so the success dialog is shown. If I use the following sequence:
.fromArray("wrong", "2", "3", "4", "5", "6", "7", "8")
Then the error dialog appears, because one of the items cannot be parsed as number. This is a correct behavior. However, if I add two incorrect items instead of one:
.fromArray("wrong", "wrong", "3", "4", "5", "6", "7", "8")
Then the application just crashes. It seems to me that the reason is because the exception happened in two threads at once, but I may be wrong in the conclusion.
When I implement the parallelization the old way, the application doesn't crash:
public void doParallelAsyncJobOldWay() {
subscription = Flowable
.fromArray("wrong", "wrong", "3", "4", "5", "6", "7", "8")
.flatMap(number ->
Flowable
.just(Integer.parseInt(number))
.subscribeOn(Schedulers.computation())
)
.toList()
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
result -> showSuccessDialog(),
error -> showErrorDialog()
);
}