Reputation: 2358
I am using RxJava with Spring boot. Now for every request, I start a say 10 child threads(using Schedulers.nextThread()).
I will be using this in production env where I can have concurrency upto 500 requests
So my question is a) Is it good practice. b) Is there any upper limit I can add to number of threads to spawn? c) How many concurrent threads can I run in Java(with say 32GB RAM) ?
So I my case, if I have 500 concurrent request and each request is spwaning 10 threads, this makes total 5500 concurrent threads.
Will that number of threads be OK in production env.
Upvotes: 2
Views: 2191
Reputation: 10152
For network calls use should Schedulers.io()
instead of Schedulers.newThread()
. Because thread creation is costly & you will hurt the performance due to context switching overhead between too many threads.
And if you need to limit the number of simultaneous network calls use Scheduler.from(Executors.newFixedThreadPool(n))
.
Upvotes: 2
Reputation: 8227
There is no good reason to spawn that many threads. The number of simultaneously executable pieces of code is limited by the number of CPU cores you have access to. Desktop machines might have two or four cores, while servers can have many tens of cores.
What you want is a thread pool, managed by an executor, such as a ScheduledExecutorService
. This can be wrapped by Schedulers.from()
to create a scheduler to be used with RxJava. In general, you would construct a thread pool with no more than twice the number of cores.
When you use the observeOn()
or subscribeOn()
operators, the scheduler that is passed in is used to select an individual thread for that observer chain.
With 500 simultaneous requests and a 20-core server, you could process no more than 20 actions at a time, meaning that you have to queue requests to be processed. If you create 5,000 or 40 threads, you can still only advance 20 requests at a time. If you create 5,000 threads you are asking the executor to handle the queuing; if you create 40 threads, the queuing is handled in the RxJava code.
Upvotes: 2