0

I have two Observables define and I am calling them as below:

Observable<Boolean> statusObser1 = reactiveApiConsumer.syncGradesSet1(subListDtos.get(0));
Observable<Boolean> statusObser2 = reactiveApiConsumer.syncGradesSet2(subListDtos.get(1));
statusObser1.toBlocking().first();
statusObser2.toBlocking().first();

But the problem is that statusObser2 executes only after statusObser1 has completed. Instead I want both observers to execute in parallel, i.e. statusObser2 shouldn't wait for statusObser1 to complete.

2 Answers 2

2

They execute sequentially since you're blocking (toBlocking()) and waiting for their response.

Instead, subscribe to them. Either individually:

Observable<Boolean> statusObser1 = ...
Observable<Boolean> statusObser2 = ...
statusObser1.subscribe(System.out::println); //Example
statusObser2.subscribe(System.out::println); //Example

Or using the Zip operator:

public static int doSomethingWithBothValues(Boolean a, Boolean b) {
    ...
}

...

Observable<Boolean> statusObser1 = ...
Observable<Boolean> statusObser2 = ...
Observable.zip(statusObser1,statusObser2, this::doSomethingWithBothValues);

see more details about the Zip operator here.

In both cases, if the observables are asynchronous, they will be executed in parallel.

There are other operators you can use to combine the results of both your operators without blocking either of them.

Sign up to request clarification or add additional context in comments.

Comments

0

Try MultiThreading.

Observable<Boolean> statusObser1 = reactiveApiConsumer.syncGradesSet1(subListDtos.get(0));
Observable<Boolean> statusObser2 = reactiveApiConsumer.syncGradesSet2(subListDtos.get(1));
startThread(statusObser1);
startThread(statusObser2);

public void startThread(Observable<Boolean> statusObser) {
    new Thread() {
        @Override
        public void run() {
            statusObser.toBlocking().first();
        }
    }.start();
}

startThread method will execute the execution in a new Thread, and both executions wil executed in seperate Thread.

1 Comment

That's a very bad solution. It completely misses the point of using Reacive programming techniques.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.