RxJava Tutorial on RxJava From Scheduler

schedulers.from(executor) method converts an executor into a new scheduler instance.

schedulers.from(executor) example

create the following java program using any editor of your choice in, say, c:\> rxjava.

observabletester.java

import java.util.random;
import java.util.concurrent.executors;

import io.reactivex.observable;
import io.reactivex.schedulers.schedulers;

public class observabletester  {
   public static void main(string[] args) throws interruptedexception {
      observable.just("a", "ab", "abc")
         .flatmap(v -> getlengthwithdelay(v)
         .doonnext(s -> system.out.println("processing thread " 
            + thread.currentthread().getname()))
         .subscribeon(schedulers.from(executors.newfixedthreadpool(3))))
         .subscribe(length -> system.out.println("receiver thread " 
            + thread.currentthread().getname() 
            + ", item length " + length));

         thread.sleep(10000);
   }
   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) {
         e.printstacktrace();
      }
      return null;
   }
}

verify the result

compile the class using javac compiler as follows −

c:\rxjava>javac observabletester.java

now run the observabletester as follows −

c:\rxjava>java observabletester

it should produce the following output −

processing thread pool-1-thread-1
processing thread pool-3-thread-1
receiver thread pool-1-thread-1, item length 1
processing thread pool-4-thread-1
receiver thread pool-4-thread-1, item length 3
receiver thread pool-3-thread-1, item length 2