Effective Thread Count

I want to optimize the number of applications in the application. Nearly all of them have IOs alongside CPU utilization in equal value. How much is the effective number of threads when there are no other applications in the system. I want to get an answer for Windows and under the JVM.

+7
java performance multithreading windows jvm
source share
6 answers

There is no answer for every OS. This will depend on the specific set of tasks your code performs. You should compare your application with different configurations to find out which one is most effective.

Some general threading tips:

  • You cannot speed up tasks with lots of threads; the exception is that if you have several processors, you can parallelize computational tasks in one thread per processor, provided that this logic can be divided so that it does not have to be executed in series. A good example for this would be a divide and win problem, such as mergesort, where the two halves can be sorted in any order.

  • You can achieve some acceleration by parallelizing tasks that do not use the same part of the machine. So, given that you say that you have β€œequal value” in the I / O and computation operations, you will want to separate them into different threads - again, this assumes that the ordering does not matter.

If this is the case (as in many applications), the threads perform some computational logic, followed by some I / O operations (for example, writing data to a disk or database server), then it will be very difficult to come up with some formula to determine the exact amount streams that you should have, as this will greatly depend on the data you process, on how you process it, and what you do with it when processing. This is why it is best to have a custom thread pool that can be easily adjusted for size β€” then do some load tests of different sizes and see which one is best done.

+12
source share

Java Concurrency in Practice gives an approximate formula for determining the size of a thread pool so that your processors are tied to a specific use:

N = number of processors

U = target CPU usage, 0 <= U <= 1

W / C = timeout ratio for calculating time

The optimal pool size (number of threads) for storing processors for the desired use:

PoolSize = N * U * (1 + (W / C))

This is for CPU use only.

You can get available processors with Runtime.getRuntime (). availableProcessors ()

+9
source share

I do not think there is a definitive answer to this question. I just suggest trying your application with a different number of threads and see which ones work best. One place to start is another thread than the number of processor threads in your hardware, for example. if you have a dual core processor with one thread per core, then use 3 threads.

+2
source share

Performance is far from the only reason for using threads.

Basically, any program with multiple threads can be modeled with one more complex thread, so what the threads actually do simplifies your code, not necessarily making it faster.

However, if your application can use multiple cores or multiple drive heads at the same time, then threads can facilitate its use. In this case, you probably don't need more threads than you have individual cores or heads, because process switching has a certain cost.

+2
source share

I found that the best way to handle this is not to use threads directly, but to use the Executor framework. You can experiment with different configurations, but I found that I liked CallerRunsPolicy.

+1
source share

There is really no universal answer to this question. The number of threads you create depends on how many tasks you perform, how they communicate and how you develop your application. I had very large applications that have only one thread that works fine. On the other hand, I also had small applications that guaranteed multiple threads to execute.

(Sorry for any problems with spelling / formatting, I dialed this from my phone)

0
source share

All Articles