Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How many threads?

Reply
Thread Tools

How many threads?

 
 
Roedy Green
Guest
Posts: n/a
 
      07-30-2008
I am about to do some multithreading code to get some parallelism when
waiting for Internet links to respond. The question comes How many
threads? Well it depend on too many things. How much ram, how fast the
connections, what else is going on in the machine.

The optimal choice could change over time, even within a single run.

What I need is some sort of homing mechanism that homes in on the
optimal number. Surely this is a common problem. Are there classes to
handle this automatically?
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
 
shakah
Guest
Posts: n/a
 
      07-30-2008
On Jul 30, 7:28 pm, Roedy Green <(E-Mail Removed)>
wrote:
> I am about to do some multithreading code to get some parallelism when
> waiting for Internet links to respond. The question comes How many
> threads? Well it depend on too many things. How much ram, how fast the
> connections, what else is going on in the machine.
>
> The optimal choice could change over time, even within a single run.
>
> What I need is some sort of homing mechanism that homes in on the
> optimal number. Surely this is a common problem. Are there classes to
> handle this automatically?
> --
>
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com


I think that your problem calls for a blocking-queue/thread-pool
solution, with possibly an upper-bound to the number of threads --
IMHO, you're unlikely to exhaust a local resource (e.g. CPU or memory,
assuming a reasonably up-to-date box) given the amount of idle time
you'll encounter waiting for I/O while making asynch requests over the
Internet.
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      07-31-2008
shakah wrote:
> On Jul 30, 7:28 pm, Roedy Green <(E-Mail Removed)>
> wrote:


>> The optimal choice could change over time, even within a single run.
>>


>
> I think that your problem calls for a blocking-queue/thread-pool
> solution, with possibly an upper-bound to the number of threads --
> IMHO, you're unlikely to exhaust a local resource (e.g. CPU or memory,
> assuming a reasonably up-to-date box) given the amount of idle time
> you'll encounter waiting for I/O while making asynch requests over the
> Internet.


Besides making use of the API in java.util.concurrent, I think you've
identified one of the requirements yourself in the line I quoted above:
change.

Think about designing for change, and which design patterns support high
change situations. The Strategy Pattern, for example. If you make your
"threading strategy" something that can be configured on the fly, then I
think you might have more ability to change things as the code (and the
user requirements) evolve.

You can start off with an ExecutorService from java.util.concurrent like
ThreadPoolExecutor, and then as you learn more substitute your own
custom, well-tuned implementation.

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      08-01-2008
Roedy Green wrote:
> I am about to do some multithreading code to get some parallelism when
> waiting for Internet links to respond. The question comes How many
> threads? Well it depend on too many things. How much ram, how fast the
> connections, what else is going on in the machine.
>
> The optimal choice could change over time, even within a single run.
>
> What I need is some sort of homing mechanism that homes in on the
> optimal number. Surely this is a common problem. Are there classes to
> handle this automatically?


I think standard practice is no make it configurable to allow
the admin to test and configure what is optimal for that site.

That is of course a Java EE way of thinking.

For your app where the actually work is relative small compared
to how long it will block, then you should go for a pretty
big number of threads. As many as the OS will still perform
well with.

Arne
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      08-01-2008
Arne Vajhøj wrote:

>
> I think standard practice is no make it configurable to allow
> the admin to test and configure what is optimal for that site.
>
> That is of course a Java EE way of thinking.


I don't think it's just "Java EE". I've seen C++ programmers go through
complex gyrations to implement something similar to Java's reflection in
C++, just because the Strategy Pattern and Data Driven Design are that
important. It something that should be considered for almost every
design. Change is the most fundamental, common aspect of all software
engineering, after all.
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      08-01-2008
Mark Space wrote:
> Arne Vajhøj wrote:
>> I think standard practice is no make it configurable to allow
>> the admin to test and configure what is optimal for that site.
>>
>> That is of course a Java EE way of thinking.

>
> I don't think it's just "Java EE". I've seen C++ programmers go through
> complex gyrations to implement something similar to Java's reflection in
> C++, just because the Strategy Pattern and Data Driven Design are that
> important. It something that should be considered for almost every
> design. Change is the most fundamental, common aspect of all software
> engineering, after all.


It is not just Java EE, but it requires a context where there are
somebody that knows about config files, threads and performance testing.

For a Java EE server there should be somebody that knows that.

For the desktop app used by Joe Average in Smalltown it is often
not the case.

Not that Java has much market share in that space anyway.

Arne
 
Reply With Quote
 
Roger Lindsjö
Guest
Posts: n/a
 
      08-03-2008
Roedy Green wrote:
> I am about to do some multithreading code to get some parallelism when
> waiting for Internet links to respond. The question comes How many
> threads? Well it depend on too many things. How much ram, how fast the
> connections, what else is going on in the machine.
>
> The optimal choice could change over time, even within a single run.
>
> What I need is some sort of homing mechanism that homes in on the
> optimal number. Surely this is a common problem. Are there classes to
> handle this automatically?


You you really need many threads? How about using nio and poll to have a
single thread manage many connections at once? This way it could be
possible just enqueue the request you need to be done and handle many of
them in parallel with a single (or few) threads.

But hundreds is often no problem. I have written a engine that emulates
user requests against a server. A "user" is one of many javascript that
can do (among other things) open(url), openLink(id), openLink(name),
sleep(time) etc. Assuming the server takes tens to hundreds of
milliseconds to respond I can usually run several hundreds of threds at
once. Thousands of threads seems to be fine if the response time goes
above one or a few seconds.

Note: I have run this on Dual and Quad core Intel and AMD on Linux, not
Windows, so there could be a difference.

--
Roger Lindsjö
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      08-04-2008
On Sun, 3 Aug 2008, Roger Lindsjö wrote:

> Roedy Green wrote:
>> I am about to do some multithreading code to get some parallelism when
>> waiting for Internet links to respond. The question comes How many
>> threads? Well it depend on too many things. How much ram, how fast the
>> connections, what else is going on in the machine.

>
> But hundreds is often no problem.
>
> Note: I have run this on Dual and Quad core Intel and AMD on Linux, not
> Windows, so there could be a difference.


I believe that linux's threading is much, much faster than windows'. But a
few hundred threads could still be fine on windows.

tom

--
Computation is the basis of all life
 
Reply With Quote
 
Christian
Guest
Posts: n/a
 
      08-04-2008
Tom Anderson schrieb:
> On Sun, 3 Aug 2008, Roger Lindsjö wrote:
>
>> Roedy Green wrote:
>>> I am about to do some multithreading code to get some parallelism when
>>> waiting for Internet links to respond. The question comes How many
>>> threads? Well it depend on too many things. How much ram, how fast the
>>> connections, what else is going on in the machine.

>>
>> But hundreds is often no problem.
>>
>> Note: I have run this on Dual and Quad core Intel and AMD on Linux,
>> not Windows, so there could be a difference.

>
> I believe that linux's threading is much, much faster than windows'. But
> a few hundred threads could still be fine on windows.
>
> tom
>


Does this believe have any base? Or is it just the usual Unix/Windows flame?
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-04-2008
On Sun, 03 Aug 2008 20:55:17 +0200, Roger Lindsjö
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>You you really need many threads? How about using nio and poll to have a
>single thread manage many connections at once? This way it could be
>possible just enqueue the request you need to be done and handle many of
>them in parallel with a single (or few) threads.


There are two issues:

1. the general one of multithread apps that don't know the optimal
number of threads, and would like some general mechanism to optimise
it dynamically to adjust for changing conditions, e.G. other tasks
running in the same machine soaking up RAM/CPU. The idea is your would
write some custom routine that measured productivity. e.G.
transactions/second, bytes processed per minute etc. The manager would
raise and lower the max threads and collect data, and try to fit it
say to a Cheyenne polynomial to try to predict the optimal value. You
would weight your readings to give more recent ones more weight in the
polynomial evaluation. It might just up the count and down the count
by one every so often just to notice if conditions are changing.


2. my specific one of improving my back end to Xenu link checker that
rechecks links in a clever way to avoid needless work. Eventually I
hope to get rid of Xenu entirely. It is too many flaws.
I am sending HEAD and GET posts out to the Internet mainly to see the
status code that comes back. I gather this nio approach works at the
socket level or lower, rather than HTTP.

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Many-to-many relationship / DataSet / child rows / DataRelation / sorting / navigation Marco Ippolito ASP .Net 0 10-11-2004 04:11 PM
Re: Cisco PIX many-to-many NAT problem Fredy Kuenzler Cisco 1 07-16-2004 07:30 PM
Cisco PIX many-to-many NAT problem Fredy Kuenzler Cisco 4 06-15-2004 07:06 PM
How many questions (avg) and How many can i miss? Josh MCSD 2 02-16-2004 07:25 AM



Advertisments