How to ensure that a set of parallel operations are all complete?

50 views
Skip to first unread message

David Karr

unread,
May 11, 2020, 3:33:40 PM5/11/20
to RxJava
We're using an old version of rxjava, 2.1.10.  I can't upgrade at this moment.

I've been writing Java code for many years, but I'm a beginner to rxjava.  Many of the services I supervise and work on use it, but I haven't had a chance to write much code using it.  I first want to understand some of the patterns I see being used in our code.

I see many examples of something like this:
    Single<ResultType> result1 = Single.<ResultType>create(source -> {
        source
.onSuccess(method1(parameters));
   
}).subscribeOn(Schedulers.io());
   
Single<ReturnType> result2 = Single.<ResultType>create(source -> {
        source
.onSuccess(method2(parameters));
   
}).subscribeOn(Schedulers.io());

   
if (null != result1 && null != result2) {

The intent of this is that the execution of "method1" and "method2" run in parallel, and that the check for "null != result1 && null != result2" happens after both methods have finished executing. I'm thinking it's possible that neither of these intentions are being fulfilled here, but I need confirmation of that, and also how to achieve those goals properly.

Dávid Karnok

unread,
May 11, 2020, 3:46:19 PM5/11/20
to David Karr, RxJava
Hi.

First of all, I recommend reading the Getting started because the assumptions you listed are wrong. https://github.com/ReactiveX/RxJava#getting-started

Second, result1 and result2 should be never null at that point but neither method1 nor method2 executes until there is a subscribe() call to both.

Third, I suggest asking such questions on StackOverflow as not many of us are watching this group.

--
You received this message because you are subscribed to the Google Groups "RxJava" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rxjava+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rxjava/c7b2871d-3b3e-4832-8b62-bae3c68acc24%40googlegroups.com.


--
Best regards,
David Karnok

David Karr

unread,
May 11, 2020, 4:39:05 PM5/11/20
to RxJava


On Monday, May 11, 2020 at 12:46:19 PM UTC-7, Dávid Karnok wrote:
Hi.

First of all, I recommend reading the Getting started because the assumptions you listed are wrong. https://github.com/ReactiveX/RxJava#getting-started
 
Care to specify which assumptions I am making are wrong?

I've now read the getting-started page, but I haven't deduced answers to my questions from that.


Second, result1 and result2 should be never null at that point but neither method1 nor method2 executes until there is a subscribe() call to both.

As you can see, I have a subscribe call for both.  I see that the way to ensure that I don't retrieve the actual results until both methods complete is to use "blockingGet()", so that answers part of my questions. What I still want confirmation of is that "method1" and "method2" will execute essentially in parallel, so if both are long-running operations (waiting on external IO, for instance), that the second operation is not waiting for the first one to complete before starting.


Third, I suggest asking such questions on StackOverflow as not many of us are watching this group.

I realized that after I posted this note, seeing the frequency of posts, which is why I posted https://stackoverflow.com/questions/61737906/how-to-ensure-that-rxjava-methods-execute-in-parallel-and-finish soon after posting this.  However, you replied to this, so at least one of you is watching. :)


David Karr <davidmic...@gmail.com> ezt írta (időpont: 2020. máj. 11., H, 21:33):
We're using an old version of rxjava, 2.1.10.  I can't upgrade at this moment.

I've been writing Java code for many years, but I'm a beginner to rxjava.  Many of the services I supervise and work on use it, but I haven't had a chance to write much code using it.  I first want to understand some of the patterns I see being used in our code.

I see many examples of something like this:
    Single<ResultType> result1 = Single.<ResultType>create(source -> {
        source
.onSuccess(method1(parameters));
   
}).subscribeOn(Schedulers.io());
   
Single<ReturnType> result2 = Single.<ResultType>create(source -> {
        source
.onSuccess(method2(parameters));
   
}).subscribeOn(Schedulers.io());

   
if (null != result1 && null != result2) {

The intent of this is that the execution of "method1" and "method2" run in parallel, and that the check for "null != result1 && null != result2" happens after both methods have finished executing. I'm thinking it's possible that neither of these intentions are being fulfilled here, but I need confirmation of that, and also how to achieve those goals properly.

--
You received this message because you are subscribed to the Google Groups "RxJava" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rxj...@googlegroups.com.

Dávid Karnok

unread,
May 11, 2020, 4:50:17 PM5/11/20
to David Karr, RxJava
>  Care to specify which assumptions I am making are wrong?

"check for "null != result1 && null != result2" happens after both methods have finished executing [...] I'm thinking it's possible that neither of these intentions are being fulfilled here"

It's not possible.

> As you can see, I have a subscribe call for both.

I don't see it. The last line I see in the email is
 if (null != result1 && null != result2) { .
subscribeOn != subscribe

> What I still want confirmation of is that "method1" and "method2" will execute essentially in parallel

Yes:

result1.subscribe(value1 -> { });
result2.subscribe(value2 -> { });

Now both method1 and method2 will execute on the IO background thread in parallel, independent of each other.

To unsubscribe from this group and stop receiving emails from it, send an email to rxjava+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rxjava/97630b78-dacc-4851-8436-3292ba0d64fc%40googlegroups.com.

David Karr

unread,
May 11, 2020, 6:35:02 PM5/11/20
to RxJava

On Monday, May 11, 2020 at 1:50:17 PM UTC-7, Dávid Karnok wrote:
>  Care to specify which assumptions I am making are wrong?

"check for "null != result1 && null != result2" happens after both methods have finished executing [...] I'm thinking it's possible that neither of these intentions are being fulfilled here"

It's not possible.

Ok, because "create()" unconditionally creates a Single object, so that will not be null.  Understood.


> As you can see, I have a subscribe call for both.

I don't see it. The last line I see in the email is
 if (null != result1 && null != result2) { .
subscribeOn != subscribe

Can you elaborate on that?  Clearly, "subscribeOn" is not equal to "subscribe", but I assumed they had similar semantics?  Can you explain?


> What I still want confirmation of is that "method1" and "method2" will execute essentially in parallel

Yes:

result1.subscribe(value1 -> { });
result2.subscribe(value2 -> { });

Now both method1 and method2 will execute on the IO background thread in parallel, independent of each other.

Understood, although I still have to get clarified how this is functionally different from what I'm doing in the code sample, using "subscribeOn()", instead of "subscribe".


Justin Breitfeller

unread,
May 12, 2020, 8:39:39 AM5/12/20
to RxJava
David,

I think you will find that reading a book is really the best way to understand the ins and outs of this library if you didn't find the resources Mr Karnok suggested helpful. I found this book to be fairly easy to digest when I get people on my team started with RxJava: https://www.amazon.com/Thomas-Nield-ebook/dp/B01N1UNBME/ref=sr_1_2?dchild=1&keywords=rxjava+2&qid=1589286981&sr=8-2
Reply all
Reply to author
Forward
0 new messages