title: RxJava Backpressure (Fast producer slow consumer)
body: <p>i have execution method which does some time consuming network calls on io thread</p>
  
  <p>example</p>
  
  <pre><code>/**
   * network call
   * @param value
   * @return
   */
  private Observable&lt;Integer&gt; execute(final int value) {
      return Observable.create(new Observable.OnSubscribe&lt;Integer&gt;() {
          @Override
          public void call(Subscriber&lt;? super Integer&gt; subscriber) {
  
              try {
                  Thread.sleep(500);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              System.out.println("done " + value);
              subscriber.onNext(value);
              subscriber.onCompleted();
          }
      }).subscribeOn(Schedulers.io());
  }
  </code></pre>
  
  <p>then i have list of "commands" which must be executed in order. (one after another)</p>
  
  <p>example (Observable.range(x,y) represents list of commands)</p>
  
  <pre><code>public List&lt;Integer&gt; testObservableBackpressure(){
     return Observable.range(0,5).flatMap(new Func1&lt;Integer, Observable&lt;Integer&gt;&gt;() {
          @Override
          public Observable&lt;Integer&gt; call(Integer integer) {
              System.out.println("started " + integer);
              return exeute(integer);
          }
      }).toList().toBlocking().single();
  }
  </code></pre>
  
  <p>in this way out put is </p>
  
  <pre><code>started 0
  started 1
  started 2
  started 3
  started 4
  done 0
  done 1
  done 2
  done 4
  done 3
  </code></pre>
  
  <p>Produses faster than is consuming</p>
  
  <p>I want results like that</p>
  
  <pre><code>started 0
  done 0
  started 1
  done 1
  started 2
  done 2
  ...
  </code></pre>
  
  <p>but..</p>
  
  <pre><code>public List&lt;Integer&gt; testObservableBackpressure(){
  
      return Observable.create(new Observable.OnSubscribe&lt;Integer&gt;() {
          @Override
          public void call(final Subscriber&lt;? super Integer&gt; subscriber) {
              Observable.range(0,5).subscribe(new Subscriber&lt;Integer&gt;() {
  
                  @Override
                  public void onStart() {
                      request(1);
                  }
  
                  @Override
                  public void onCompleted() {
                      subscriber.onCompleted();
                  }
  
                  @Override
                  public void onError(Throwable e) {
                      subscriber.onError(e);
                  }
  
                  @Override
                  public void onNext(Integer integer) {
                      System.out.println("started " + integer);
                      execute(integer).subscribe(new Action1&lt;Integer&gt;() {
                          @Override
                          public void call(Integer integer) {
                              subscriber.onNext(integer);
                              request(1);
                          }
                      });
                  }
              });
          }
      }).toList().toBlocking().single();
  }
  </code></pre>
  
  <p>this way results are as expected </p>
  
  <pre><code>started 0
  done 0
  started 1
  done 1
  started 2
  done 2
  started 3
  done 3
  started 4
  </code></pre>
  
  <p>My question would be if there is another more elegant way to handle this problem?</p>