I'd not be asking the question of how many threads is too many, but more
what are those threads doing, and can they be merged into one?
If your actions in each thread take a second to execute, and it needs to
be done every second then a single thread will not be fast enough as you
need to have four seconds to execute the four actions. However if those
actions only take miliseconds, or less then it would make sense just to
run them one after another. Additionally, is each of these actions using
CPU? or is it mostly low CPU/IO that is happening? If each thread uses
100% CPU for the second it runs in, threading would be pointless on
anything other than a quad CPU or dual core dual cpu system - each thread
would be competing for CPU to process.
For example, my application is going to find every computer on the network
and retrieve its computer name and logged in user assume this takes 3
seconds to do for each PC. If i did this all one computer after another
this would take a huge amount of time on a network with 100-200 PC's -
this is five to ten minutes to refresh the network list - pretty long.
However if i were to create a set of 60 worker threads thats handled the
100-200 PC's then you would be getting 60 PC's per 3 seconds. Thats 5-10
seconds for the list to refresh - much more sensible. However the 3
seconds it needs to wait is mostly network latency and computers talking
to each other, 0.02% cpu per thread if your lucky, and a few bytes/second
over the network. This situation is ideal for threading. A situation that
is not idea for threading would be scanning the hard drive (lots of IO
Wait) or performing complex math, eg drawing fractals (lots of CPU wait).
Hope this helps
- Mark
On Tue, 07 Mar 2006 14:36:51 +0800, Alexander Walker
<al**@noemail.n oemail> wrote:
Hello
Is it inefficient to create an application that has many threads that
individually may do a small amount of work over a given period of time
as
opposed to an application that has a smaller number of threads that do a
larger
amount of work over a given time period
here is an oversimplified example
application 1 has 4 threads
every second each thread makes an http request for a file on a different
server
and then saves the file it requested to disk
application 2 has 1 thread
every second the thread makes four http requests for a file to four
different
servers one after the other after each request it saves the file to disk
you could say that application 1 and application 2 do the same thing,
but they
do it in different ways
how expensive is it to switch between threads?
image application 1 and 2 where doing something other than requesting
files,
such as reading or writing from a database or executing some operations
on data
I ask this because I have a windows service that creates 4 threads, the
number
of threads it creates is variable so in the future it might create more
threads
or less threads, each thread performs an operation every second, should
I be
concerned about how many threads I'm creating? I think that I should,
but I'm
not sure how to determine how many threads is too many
thanks
Alex