Thank you for your post.
> Thank you for your post.
>> Also, how many hardware execution units ("CPU's," "cores,"
[quoted text clipped - 3 lines]
> Memory size: 4GB
> Operating system: Solaris 9
You don't mention the CPU type, which matters a lot,
but in any event the number of threads that can execute
simultaneously on the hardware will not be close to a
thousand. You could likely count them on your fingers;
taking your shoes off is probably unnecessary.
> Why didn't the Executor recycle through the threads created via:
> ExecutorService pool = Executors.newFixedThreadPool(1000)?
Why do you think it didn't? My suspicion (until proven
otherwise, and proofs are certainly possible) is that it
never got to a thousand in the first place, but ran out of
memory before it could create that many threads.
> The ExecutorService is declared in the class field and not in the
> doPost() method, will this be an issue?
It's in the Foo constructor (well, sort of: what you
showed is obviously a paraphrase -- it wouldn't compile,
for starters -- so I can't be sure just where the pool
is created). But let's suppose it's in Foo's constructor,
and that Foo is an HTTPServlet. That means you'll get a
new pool each time an HTTPServlet/Foo instance is created.
I don't know how frequently Tomcat does so -- I seem to
recall that it ordinarily creates a given servlet type
only once, but that there may be ways of unloading and
reloading them, or ways of starting multiple instances
under different aliases. So you'll have at least one pool
with a "ceiling" of a thousand threads, possibly N such
pools.
I say again: Reduce the thread count, maybe to ten or
twenty-ish. What benefit do you expect from a thousand
threads all trying to elbow each other out of the way for
the attention of two processor chips?

Signature
Eric.Sosman@sun.com
Alex.From.Ohio.Java@gmail.com - 19 Mar 2008 01:14 GMT
> You could likely count them on your fingers;
> taking your shoes off is probably unnecessary.
Eric! You are Sun guy! Open JConsole and look how many threads simple
application has! I never saw less then 20. Should I mention all of
them? :)
Alex.
http://www.myjavaserver.com/~alexfromohio/
Knute Johnson - 19 Mar 2008 01:32 GMT
>> You could likely count them on your fingers;
>> taking your shoes off is probably unnecessary.
[quoted text clipped - 4 lines]
> Alex.
> http://www.myjavaserver.com/~alexfromohio/
On my Windows machine, more than about 100 threads will bring it to a
halt. Running Linux I can get two or three times that many but there is
no real performance advantage. You are probably better of with 10 or 20
threads in your Executor.

Signature
Knute Johnson
email s/nospam/linux/
Owen Jacobson - 19 Mar 2008 05:26 GMT
On Mar 18, 8:14 pm, Alex.From.Ohio.J...@gmail.com wrote:
> On Mar 18, 5:19 pm, Eric Sosman <Eric.Sos...@sun.com> wrote:> You could likely count them on your fingers;
> > taking your shoes off is probably unnecessary.
[quoted text clipped - 4 lines]
>
> Alex.http://www.myjavaserver.com/~alexfromohio/
Threads are _not free_. Threads that are "ready to run" are
especially not free.
Each thread has a stack, which requires memory from the process's
address space (which is largely taken up by Java's heap). Each ready
thread must be considered by the scheduler every time the scheduler
runs. Thread context switches save the state of the processor to the
stack and load the new thread's state from the new stack, effectively
resetting the CPU cache.
The ideal application has exactly one ready-to-run thread per
execution unit, not hundreds. In practice it's hard to hit this ideal
goal, so often a small integer number of threads are used per core
instead - two is a fairly popular rule of thumb.
Yes, your system is probably running close to a hundred threads
already. The vast majority of them are waiting most of the time -
either for IO, or for user input - and are therefore not causing
context switches nor being considered by the scheduler. Furthermore,
these threads are scattered across many processes, so the address
space contention threads can cause in a single process is a much
smaller concern.
One app trying to run a thousand threads simultaneously is grossly
wasteful. I'm not at all surprised that it doesn't work on your
system.
Eric Sosman - 19 Mar 2008 14:03 GMT
>> You could likely count them on your fingers;
>> taking your shoes off is probably unnecessary.
> Eric! You are Sun guy! Open JConsole and look how many threads simple
> application has! I never saw less then 20. Should I mention all of
> them? :)
In the material you snipped, I made it clear that I was
referring to "hardware execution units," to "threads that
can execute simultaneously on the hardware." Some people
call these "strands" or "pipelines," but neither term seems
entirely satisfactory to me. What I was getting at was:
How many threads could possibly be in the act of executing
instructions at a given moment? This is not the same as:
How many threads could exist on the system and be eligible
to compete for the right to execute instructions?
The two notions -- "strands" and threads -- are different
and to some degree independent (the system's scheduler maintains
the illusion that all eligible threads run simultaneously, even
though there may be more threads than "strands"). However, it
is usually a sign that something is wrong if the number of
eligible threads vastly exceeds the number of "strands" that
can run them.

Signature
Eric Sosman
esosman@ieee-dot-org.invalid