Is there any number of threads should be used for the best performances in an application?

99 views Asked by At

I've heard that there is a number of threads in an application which used be used to get the best performances. I've heard that when the number of thread are increased, the performances will increase until one point and after that it will start to decrease. And for android application that limit is like 3 or 4.

Can someone explain this inside out ?

Currently I'm working on a C# standalone application and in there I've used about 50 background workers. How this affect to the performances of the system ?

2

There are 2 answers

0
garglblarg On

As mentioned before, there is no definitive answer to this but i might as well share a bit of experience here. If you're working on a standalone client application it's usually a good idea to have one thread for core functionality and the user interface and separate threads to perform things like prime factorization. The point of this is to keep the UI responsive even if your background worker is really busy crunching numbers.

Beyond that it might be a good idea to assign tasks to new threads, so that you can manage them properly but i'd suggest this only if there are a lot of different tasks to keep track of and you already know that some of those threads could run into infinite loops or that you want a way to shut them down without halting the whole application.

to get started with the theoretical side of things you might want to check out: Amdahl's Law

best regards, garglblarg :>

1
Marc Gravell On

There is no single answer to this. It depends on what your app is doing, and what the bottlenecks are. If your app is doing lots of CPU work and is pegging the device, then "the number of cores" is your limiting factor (going beyond this will simply increase switching); if it is mostly waiting on disk / network, then 1 might be more than enough.

Adding threads is not a magic bullet - and can be positive or negative.