有效的线程数 [英] Efficient Number of Threads

查看:80
本文介绍了有效的线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想优化我的应用程序线程数。
几乎所有的IO都具有相同值的CPU使用率。
当系统中没有其他应用程序运行时,有效线程数是多少。
我想要Windows和JVM下的答案。

I want to optimize my application number of threads. Almost all of them have IO beside CPU usage in an equal value. How much is the efficient number of threads when there are no other applications running in system. I want the answer for Windows and under JVM.

推荐答案

没有每个操作系统的答案。这取决于代码执行的特定任务集。您应该使用不同的配置对应用程序进行基准测试,以确定哪个是最高性能的。

There is no per-OS answer. This will depend on the specific set of tasks that your code is performing. You should benchmark your application with different configurations to see which one is the most performant.

有关多线程的一些常规提示:

Some general tips about multithreading:


  • 你无法加速具有更多线程的任务;例外情况是,如果您有多个CPU,则可以将计算任务与每个CPU的一个线程并行化,前提是可以拆分此逻辑,使其不一定需要按顺序执行。一个很好的例子就是像mergesort这样的分而治之的问题,其中两半可以按任何顺序排序。

  • You can't speed up like tasks with more threads; the exception is that if you have multiple CPUs, you can parallelize compute tasks with one thread per CPU, provided this logic can be split up such that it does not necessarily need to be executed serially. A good example for this would be a divide-and-conquer problem like mergesort, where the two halves can be sorted in any order.

你可以实现一些通过并行化不使用机器相同部分的任务来加速。因此,假设您说I / O和计算任务具有相同的价值,您将需要将它们分成不同的线程 - 再次,这假设排序并不重要。

You can achieve some speedup by parallelizing tasks that do not make use of the same part of the machine. So, given that you say you have "equal value" of I/O and compute tasks, you will want to separate those into different threads - again, this assumes that ordering is not important.

如果是这种情况(与许多应用程序一样),线程执行一些计算逻辑,然后执行一些I / O(比如将数据写入磁盘或数据库)服务器,例如)然后很难想出一些公式来确定你应该拥有的确切线程数,因为这将高度依赖于你正在处理的数据,你如何处理它,以及你是什么处理完成后正在处理它。这就是为什么要做的最好的事情是有一个可配置的线程池,其大小可以很容易地调整 - 然后运行一些不同大小的负载测试,看看哪一个表现最好。

If it is the case (as with many applications) that threads perform some compute logic followed by some I/O (like writing the data to disk or a database server, for example) then it will be very difficult to come up with some formula to determine the exact number of threads you should have, as this will be highly dependent on the data you are processing, how you are processing it, and what you are doing with it when processing is done. This is why the best thing to do is have a configurable thread pool whose size can be adjusted easily - then run some load tests with different sizes and see which one performed best.

这篇关于有效的线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆