Is there any max value? Possibly because the underlying OS does allow only 'X' number of threads per process, etc.?
-
I agree with TechExchange answer, and I may add that if you are wondering about this issue, then you might also be interested in things like "fixed thread pools". – gd1 Jan 17 '13 at 13:01
3 Answers
This depends on the CPU you're using, on the OS, on what other processes are doing, on what Java release you're using, and other factors. I've seen a Windows server have > 6500 Threads before bringing the machine down. Most of the threads were not doing anything, of course. Once the machine hit around 6500 Threads (in Java), the whole machine started to have problems and become unstable.
My experience shows that Java (recent versions) can happily consume as many Threads as the computer itself can host without problems.
Of course, you have to have enough RAM and you have to have started Java with enough memory to do everything that the Threads are doing and to have a stack for each Thread. Any machine with a modern CPU (most recent couple generations of AMD or Intel) and with 1 - 2 Gig of memory (depending on OS) can easily support a JVM with thousands of Threads.
If you need a more specific answer than this, your best bet is to profile.

- 15,480
- 4
- 33
- 57
There is always some maximum value, but what it is and how it is determined varies wildly. Among other things, the limit can be implicitly imposed by the total memory allocation pool limit on the JVM or by explicit OS-level restrictions. One way to get around the memory limit is to configure the JVM with lower stack size.
See this answer to find out how the limit is determined on Linux.

- 1
- 1

- 195,646
- 29
- 319
- 436
-
I would say `total virtual memory limit` as the process don't know how much RAM is being used. – Peter Lawrey Jan 17 '13 at 13:07
-
1
-
@MarkoTopolnik Thanks.. I accept this answer with an understanding that there is a max value which varies with the OS, JVM and/or other parameters. Also from other answers, I understand that such a value is quite big and need not be worried about. – Pravin Sonawane Jan 18 '13 at 07:04
On Linux I have seen a maximum of ~32000 after which it fails to create new threads. If you have CPU bound tasks the optimal number of threads is likely to be the number of CPUs you have so it's generally not a good idea to have anything like this number.

- 525,659
- 79
- 751
- 1,130