Search code examples
multithreadingrx-javaobservableschedulerthreadpoolexecutor

ThreadpoolExecutor with Observables


Hi I am currently using Schedulers.io() with Observables for my api which makes network calls. I have a concern that in production it might create a lot of threads if there is huge volume of requests. I am expecting about 500k - 700k requests per day. Is Schedulers.io() a good candidate in this scenario.

Or should I create a custom threadpoolExecutor and use it as Schedulers.from(myExecutor)

    //Sample
    ThreadPoolExecutor myExecutor= new ThreadPoolExecutor(corePoolSize, 
    maxPoolSize, poolKeepAliveInMillis, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(corePoolSize));

All the examples that I saw online used a fixed thread pool like Schedulers.from(Executors.newFixedThreadPool(n)).

Does Rx java Schedulers support ThreadPoolExecutor? Which one is the best approach, please advice.


Solution

  • RxJava can use various kinds of schedulers, using Schedulers.from(threadPoolExecutor) for instance. In one project, there was a strong requirement on threads that they have a given name, often with a number, and that they have an exception catcher. It was easy to use Schedulers.from() to repurpose the executor into a scheduler.

    Also, the Schedulers.io() schedulers recycle threads fairly efficiently. You can do a thread dump after a lot of transactions to see that the number of threads is relatively limited.