Reputation: 1833
Is there any max value? Possibly because the underlying OS does allow only 'X' number of threads per process, etc.?
Upvotes: 3
Views: 4127
Reputation: 200246
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.
Upvotes: 3
Reputation: 533790
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.
Upvotes: 2
Reputation: 15768
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.
Upvotes: 4