Some sources say, that you have a hard limit to a number of threads in Java (like 15k or 30k) even if you have no OS cap per that and unlimited RAM. I also heard, that in Java 7 this limit is lifted. Are both statements true?
-
2It seems like an easy thing to figure out experimentally. – Justin Dearing Mar 25 '11 at 20:32
-
1@Justin Dearing: How? Do you have a machine with unlimited RAM to go over 212000 threads for example (default CentOS 64bit kernel maximum)? With 64k memory per thread's stack it will be like 800Gbs. I have no such PC in possession ;) – Vladislav Rastrusny Mar 25 '11 at 20:41
-
Afaik limit is set by the `OS`, not the `JVM` – Johan Sjöberg Mar 25 '11 at 20:42
-
I saw the limit mentioned here: http://paultyma.blogspot.com/2008/03/writing-java-multithreaded-servers.html and I wonder if this is true. – Vladislav Rastrusny Mar 25 '11 at 20:43
-
I couldn't get above around 2500 on Windows 7 64 bit, 12 GB RAM and 8 core. – Mark Peters Mar 25 '11 at 20:52
-
@Mark because of what? What error do you get, what stack size do you use? – Vladislav Rastrusny Mar 26 '11 at 10:03
1 Answers
The Java Virtual Machine Specification doesn't specify a limit on the number of threads. They are typically limited by the amount of stack space available, since each thread gets its own private stack. (The inability to allocate a stack is what usually triggers an OutOfMemory exception when trying to create a new Thread.) I believe that thread pools and other mechanisms can be used to also limit the number of threads. (This is used, for example, by the Sun Java System Portal Server to throttle the number of transactions.)
Different virtual machine implementations may impose other constraints. For instance, the BlackBerry OS restricts non-system threads to 16 per application and 64 total. I expect that other VM makers have also imposed additional constraints. I'd check with the VM manufacturer for an answer to this.

- 232,168
- 48
- 399
- 521
-
@Ted then Paul Tuma mistakes here: http://paultyma.blogspot.com/2008/03/writing-java-multithreaded-servers.html ? – Vladislav Rastrusny Mar 25 '11 at 20:48
-
Whether he's right or wrong, a thread is big. It's unlikely you really need 30K objects of that size to represent whatever the live entities are (connections, sessions, users, etc.) Callback schemes can scale better. – Vance Maverick Mar 25 '11 at 23:42
-
I'm guessing you can't get about Integer.MAX_INT threads due to the thread scheduler (or thread group) holding threads in collections with a int size field. – MeBigFatGuy Mar 26 '11 at 04:32
-
@MeBigFatGuy probably, yes, but what about lower limits like 15k or 30k as mentioned by Paul? – Vladislav Rastrusny Mar 26 '11 at 10:04
-
@Vance Well, this was a theoretic question. "You will unlikely need XXX" just doesn't satisfy my greedy-for-knowledge mind ;) – Vladislav Rastrusny Mar 26 '11 at 10:06
-
@FractalizeR - Paul Tyma is talking about the thread limit imposed by a specific VM implementation. (He even points out that the limit varies with the VM). In other words, this is a question about implementation limits, not limits inherent in the Java language. – Ted Hopp Mar 27 '11 at 00:48
-
@Ted I see now, thanks. I wonder what is that VM implementation with such limit imposed... – Vladislav Rastrusny Mar 27 '11 at 19:09
-
@TedHopp Hello,after read your last paragraph in which you said in BB OS we can have only 16 non_system thread per application.I worry about thing i have work for OS 5.0 ,now does this BB OS 5.0 have still some limit in Number Thread for Use.I need more then 16 Thread can be start in my application does that cause problem in My Application. – Herry Mar 22 '12 at 07:21
-
@Herry - According to [this answer to a similar question](http://stackoverflow.com/a/4260212/535871), the thread limit was removed in BB OS 5.0. I haven't tracked down official documentation to back that up, but I suspect that it is correct. – Ted Hopp Mar 22 '12 at 14:41
-
@TedHopp : I could not find url for JVM Spec, please update from here http://docs.oracle.com/javase/specs/jvms/se7/html/index.html – Nandkumar Tekale Jul 18 '12 at 07:22