RxJava - Computation Scheduler

RxJava - Computation Scheduler

Computation — This scheduler is quite similar to IO Schedulers as this is backed by thread pool too. However, the number of threads that can be used is fixed to the number of cores present in the system. So if you have two cores in your mobile, it will have 2 threads in the pool. This also means that if these two threads are busy then the process will have to wait for them to be available. While this limitation makes it a poor fit of IO related things, it is good for performing small calculations and are generally quick to perform operation


public class RxSchedulers {
    public static void main(String[] args) throws InterruptedException
         Observable.just("Rx", "Java", "Scheduler")
         .flatMap(v -> getLengthWithDelay(v)
         .doOnNext(s -> System.out.println("Processing Thread " 
            + Thread.currentThread().getName()))
         .subscribe(length -> System.out.println("Receiver Thread " 
            + Thread.currentThread().getName() 
            + ", Item length " + length));

   protected static Observable<Integer> getLengthWithDelay(String v) {
      Random random = new Random();
      try {
         Thread.sleep(random.nextInt(3) * 1000);
         return Observable.just(v.length());
      } catch (InterruptedException e) {
      return null;




Processing Thread RxComputationThreadPool-1
Receiver Thread RxComputationThreadPool-1, Item length 2
Processing Thread RxComputationThreadPool-2
Receiver Thread RxComputationThreadPool-2, Item length 4
Processing Thread RxComputationThreadPool-3
Receiver Thread RxComputationThreadPool-3, Item length 9