Single thread pool vs one thread pool per task

别说谁变了你拦得住时间么 提交于 2019-12-01 20:23:14

You have to consider which parts of the processing will benefit from parallelism. The online API communication will most likely be a candidate, since there will be sockets and network waits involved. Likewise with the DB interaction. Multithreaded parsing will probably only improve performance if there are multiple available CPU cores.

Splitting the entire process into 3 separate classes will definitely increase the cohesion, meaning each class will have less responsibilities, which is a good thing. On the other hand, making each of these classes a Runnable and having several queues will increase the complexity (possibly unecessarily) of the application.

I would suggest making 3 separate classes, but dont make them Runnable. Then make a Runnable that contains and orchestrates the 3 classes, that is one single thread pool. If you see that this doesnt seem to be fast enough (and after some profiling), try splitting the runnable into 2 thread pools: a download and parse, and a db access.

The point being, start simple and add complexity as needed.

One important thing to consider: does the order of the processing matter? i.e., is it important that the parsed result from the first download request gets loaded into the DB before the results from the second request?

If so, you really need queues (or similar), one per task. In effect, three single-threaded thread "pools" (or use an ExecutorService).

If not, @Brady makes good points. Unlike him, I'd probably make all three classes Runnable, but that doesn't mean you have to use three queues, you could still try a single pool and profile to see how it is working.

I don't believe there is a standard approach, it depends on your requirements.

If you are writing something quick and dirty then you're best having one pool.

If you're looking for something more resilient and where recovery is required then you may opt for several pools. Eg. if you persist the responses and if your app dies then when it restarts you can just re-queue the responses without having to fetch them again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!