473,804 Members | 3,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How many threads is too many?

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
Mar 7 '06 #1
6 5402
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


Mar 7 '06 #2
Hi Alex,

Commonly the thread count depends on the concrete scenario in the operating
system and your concrete program request.
The Threads is scheduled by the Operating system. The OS will control when
to switch to another thread, and the thead may not be the second thread you
created in your process.

e.g. On a OS, there are many threads and many resources are occupied. In
this scenario, if you create a few threads, the performance may not
improve, maybe even worse.
or
You have a job which will finished in a short time which is not CPU
consuming. We do not need to handle it with more threads.
For you scenario, I think you may use a ThreadPool in your windows
Service, which will help to schedue your threads.
Here is link for your reference.
Programming the Thread Pool in the .NET Framework
http://msdn.microsoft.com/library/de...us/dndotnet/ht
ml/progthrepool.as p

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 7 '06 #3
Just something to keep in mind. For the near future computing speed is
going to be gotten by going to multi-core computers. We will be seeing 4
computers on on chip soon and who knows how many more that will go up
too. Single thread applications will not take advantage of this
technology. So your application 2 would not take advantage of the new
technologies coming to your desktop soon. Of course this doesn't mean
hundreds of threads is a good thing.

Just something to think about :)
Leon Lambert

Alexander Walker 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

Mar 7 '06 #4
Note that this assumes that your application has only one single thread,
which is not the case in .NET where all applications have three threads to
start with. Something else to think about is that your application is not
the only one running on a system like windows, other processes may have
threads that need to be scheduled as well, in reality, you will never have
as many CPU's as there are (run-able)threads in a system, so a thread will
stay a precious resource and thread switching will remain a (too) costly
operation.

Willy.
"Leon Lambert" <la******@inil. com> wrote in message
news:u8******** ******@TK2MSFTN GP10.phx.gbl...
| Just something to keep in mind. For the near future computing speed is
| going to be gotten by going to multi-core computers. We will be seeing 4
| computers on on chip soon and who knows how many more that will go up
| too. Single thread applications will not take advantage of this
| technology. So your application 2 would not take advantage of the new
| technologies coming to your desktop soon. Of course this doesn't mean
| hundreds of threads is a good thing.
|
| Just something to think about :)
| Leon Lambert
|
| Alexander Walker 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
| >
| >
Mar 7 '06 #5
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Note that this assumes that your application has only one single thread,
which is not the case in .NET where all applications have three threads to
start with. Something else to think about is that your application is not
the only one running on a system like windows, other processes may have
threads that need to be scheduled as well, in reality, you will never have
as many CPU's as there are (run-able)threads in a system, so a thread will
stay a precious resource and thread switching will remain a (too) costly
operation.


It's also not much use if each thread is pretty much idle. :-)

Michael
Mar 7 '06 #6

"Leon Lambert" <la******@inil. com> wrote in message
news:u8******** ******@TK2MSFTN GP10.phx.gbl...
Just something to keep in mind. For the near future computing speed is
going to be gotten by going to multi-core computers. We will be seeing 4
computers on on chip soon and who knows how many more that will go up too.
Single thread applications will not take advantage of this technology.


You are making the rather rash assumption that your computer has nothing
better to do than run your app realy fast.

Last time I looked at the task manager there were over 100 processes running
and there will probably be more in future :-(

Most apps I've ever written are I/O bound anyway so provided that you don't
block your threads on a network call when there is something useful to be
done (usualy the GUI) then it doesn't realy make much difference adding more
threads except where it makes the code easier to understand.
Mar 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4207
by: Przemysław Różycki | last post by:
Hello, I have written some code, which creates many threads for each connection ('main connection'). The purpose of this code is to balance the load between several connections ('pipes'). The number of spawned threads depends on how many pipes I create (= 2*n+2, where n is the number of pipes). For good results I'll presumably share main connection's load between 10 pipes - therefore 22 threads will be spawned. Now if about 50
34
10819
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; public class A {
6
2528
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the data is collected is that we have different schedules (so one set of data is collected say every second, another set of data might be collected every 30 seconds, and so on).
3
5976
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional ThreadPool thread. And that is exactly what MS VIsual Studio shows. But when I run Processexplorer or Taskmanager I see 2 additional threads, after a while another 2 additional threads. With the 3 threads at start time we have totally 7 threads.
0
10562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10070
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.