2

I am using Rxjava2 with spring boot.

I have 500 concurrent request on server.

Each request spawns 10 threads which calls other services(so IO work)

So in this case, should I use Schedulers.io() or Schedulers.compuatation().

Basically my confusion is ideally io() should be used as this is IO work, but this could create large number of threads?

Also can i specify the pool size of computation threads? Also can i specify the pool size of io threads?

iagreen
  • 31,470
  • 8
  • 76
  • 90
Ankit Bansal
  • 2,162
  • 8
  • 42
  • 79
  • 1
    this doesn't answer your specific question, but if you're looking for more control over the number of threads created, you can use `Schedulers.from(Executor)` with an executor instance configured with a custom thread management policy. – homerman Apr 07 '18 at 00:00

1 Answers1

5

should I use Schedulers.io() or Schedulers.compuatation().

You want to calls other services, it's I/O work, so you should not use computation(). Because it's best to leave computation() for CPU intensive work only otherwise you won't get good CPU utilization.

can i specify the pool size of computation threads?

No, computation() is backed by a bounded thread-pool with size equal to the number of available processors. So if you want to spawns 10 threads, you cannot do it.

can i specify the pool size of io threads

If you need to limit the maximum number of simultaneous network calls, use: Scheduler.from(Executors.newFixedThreadPool(10))

It's unnecessary for your use-case, because you only do 10 task at a same time. But it's a good practice, because io() is unbounded and if you need to schedule hundreds tasks in parallel then each of them will have their own thread and causes context switching overhead.

For more information, see: rxJava Schedulers Use Cases .

nhoxbypass
  • 9,695
  • 11
  • 48
  • 71
  • 1
    can you tell me more about what is CPU intensive work? everywhere written the same. I am confused, does meaning network operations or read-write work? – iamkdblue May 07 '19 at 11:28
  • No one explains what can be termed as CPU intensive work, someone will say when processing huge data but no one will answer huge data means how much and in what conditions? – Swapnil Kadam Aug 15 '21 at 14:27
  • CPU intensive is something that takes a long time to operate (manipulate bitmap,...). – nhoxbypass Aug 21 '21 at 14:24
  • I'm not so expert in RxJava, but I think there actually IS a way to modify the pool size of Scheduler.computation ([see Baeldung](https://www.baeldung.com/rxjava-schedulers#computation)): _If for some reason, we need a different number of threads than the default, we can always use the_ `rx.scheduler.max-computation-threads` _system property_ – Marco Carlo Moriggi Oct 01 '21 at 10:00