Re: Unlimited threads being created
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 li=
kely 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/
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.