如何选择线程池大小? [英] How to choose thread pool size?
问题描述
假设我必须阅读,处理和更新Java中的许多文件.我将使用一台16核的计算机.由于我同时具有IO绑定(读取和更新文件)和CPU绑定(处理)任务,因此我分配了2个线程池.
Suppose I have to read, process and update a lot of files in Java. I am going to use one computer with 16 cores. Since I have both IO-bound (read and update files) and CPU-bound (processing) tasks I allocate 2 thread pools.
我将为具有16个线程(线程数== CPU数)的CPU绑定任务分配一个池.现在,我想知道IO绑定池的大小是多少. 您会建议使用什么大小的线程池?
I would allocate one pool for CPU-bound tasks with 16 threads (the number of threads == the number of CPUs). Now I wonder what the IO-bound pool size is. Thread pools of what sizes would you suggest ?
推荐答案
这将取决于您的存储功能和您要尝试的IO类型.例如,在硬盘上进行长时间的顺序写入会偏向于单个IO线程,但是您可能希望根据自己的需求进行扩展.
It would depend on your storage capabilities and what kinds of IO you are trying to do. For example, long sequential writes on harddisks would favor a single IO thread, but you would want to scale this according to your requirements.
在这个答案中, https://stackoverflow.com/a/2821025/2855891 ,BlackAura解释了为什么进行实验和剖析可能是您真正了解的唯一方法.
In this answer, https://stackoverflow.com/a/2821025/2855891, BlackAura explains why experimentation and profiling is probably the only way you can really find out.
关于该主题的文章可能已经非常好了.
There are probably already very good articles on this topic.
这篇关于如何选择线程池大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!