RxJava selection scheduler

premise:

There are in RxJava in 5 different schedulers are available:

  1. load immediate () : Creates and returns a Scheduler to perform work on the current thread immediately.
  2. Trampoline () : Creates and returns a Scheduler, Scheduler where the thread does not work immediately, but wait until after the end of our wait time can be set to perform (the default is 0), of course, this is to delay setting achieve internal set at Runnable. Another point is that all of the tasks to be added to a queue, and then turn to perform.
  3. newthread () : Creates and returns a Scheduler, create a new Thread for each task.
  4. Computation () : Creates and returns Scheduler is used to calculate work. It can be used in the event loop, handle the callback and other calculations. Be careful not to use this type of IO Scheduler to perform the work, which we can use io () instead.
  5. IO () : Creates and returns IO Scheduler for the type of work. This implementation maintains a thread pool Executor, the thread pool can grow as needed. The Scheduler can be used to perform asynchronous blocking IO. Do not use the Scheduler to perform computing tasks.

problem:

The first three Scheduler explain the very place of computation () and io () a little confused.

  1. What exactly is "IO-bound work"? It is suitable for processing STREAMS ( java.io) and Files ( java.nio.files) do? Suitable for database query it? Apply to download files or access the REST API?
  2. computation () and newThread () What is the difference between the two?
  3. Why call during IO task computation () would be bad?
  4. Why call when performing computing tasks io () would be bad?

answer:

Great question, I think the document can provide more details.

  1. io()Unlimited number of thread from the thread pool support for the implementation of non-compute-intensive tasks, these tasks will not cause too much burden on the CPU (such as north and south bridge chip, south bridge chip on the motherboard is responsible for the floppy drive, hard disk, keyboard and additional card data exchange). Therefore, the interaction with the file system, database or interactive services on different hosts and is well suited scene.
  2. computation()Of a limited number of support thread pool size equal to the number of available processors. If you try to schedule parallel cpu intensive (such as using available outside the processor newThread()), then when the thread contention processor, you will face the overhead of thread creation and context switching overhead, and it may be greatly affected performance .
  3. Leaving only the best computation()CPU-intensive work, otherwise you will not be able to get a good CPU utilization.
  4. io()According to 2 in spoken use when performing computing tasks io()is very good if you io()parallel arrange a thousand computing tasks, then this one thousand missions each will have its own thread and compete for CPU generated context switching costs.

Guess you like

Origin juejin.im/post/5d10d2e56fb9a07f091baf7d