title: Process a list of IO intensive tasks in parallel with RxJava
body: <p>I need to get some files in parallel. The get operation itself is IO intensive, and can benefit greatly from parallel execution.</p>
  
  <p>With RxJava I was able to achieve this by wrapping my function with <code>Async.toAsync</code>.</p>
  
  <p>I was wondering if there would be a cleaner way with using <a href="http://reactivex.io/RxJava/javadoc/rx/Observable.html#subscribeOn(rx.Scheduler)" rel="nofollow">subscribeOn()</a>, or <a href="http://reactivex.io/RxJava/javadoc/rx/Observable.html#observeOn(rx.Scheduler)" rel="nofollow">observeOn()</a>? I wasn't able to figure it out. Tried different ways, but only one thread would be used any way, and the processing happened sequentially.</p>
  
  <pre class="lang-java prettyprint-override"><code>import rx.Observable;
  import rx.Scheduler;
  import rx.functions.Func1;
  import rx.schedulers.Schedulers;
  import rx.util.async.Async;
  import java.util.List;
  import java.util.concurrent.Executors;
  import java.util.concurrent.Future;
  import java.util.concurrent.TimeUnit;
  
  public class ParallelMultiGet {
  
      public List&lt;String&gt; readContents(List&lt;String&gt; paths) {
  
          Func1&lt;String, String&gt; getFunction = new Func1&lt;String, String&gt;() {
              @Override
              public String call(String path) {
                  return get(path);
              }
          };
  
          Scheduler scheduler = Schedulers.from(
                  Executors.newFixedThreadPool(
                          Math.min(paths.size(), 50)));
  
          Future&lt;List&lt;String&gt;&gt; result = Observable
                  .from(paths)
                  .flatMap(
                          Async.toAsync(getFunction, scheduler))
                  .toList()
                  .toBlocking()
                  .toFuture();
          try {
              return result.get(30, TimeUnit.MINUTES);
          } catch (Exception e) {
              // For example if Func1.call above throws an exception it ends up in here
              throw new IllegalStateException("Couldn't read paths", e);
          }
      }
  
      private String get(String path) {
          // this would be the slow operation, waiting for IO
          return "content";
      }
  
  }
  </code></pre>
  
  <p>Even this is quite nice already, because I don't need to build my own loops that submit futures and combine values from them to the result list. But maybe it wouldn't have to be this verbose?</p>