McGovernTheory asked this question in StackOverflow:
How many threads does a Java virtual machine support at most? Is it related to virtual machine developers? What about the operating system? Are there any other factors?
Eddie's answer:
It depends on the CPU you are using, the operating system, what other processes are doing, the version of Java you are using, and other factors. I've seen a Windows server with more than 6500 threads before it goes down. Of course, most threads do nothing. Once there are almost 6500 threads (in Java) on a machine, the machine will start to have problems and become unstable.
In my experience, the threads contained in the JVM are positively correlated with the performance of the computer itself.
Of course, you need to have enough native memory and allocate enough memory to Java so that each thread can have a stack (virtual machine stack) and can do whatever you want. Any machine with a modern CPU (AMD or Intel's recent generations) and 1-2G memory (depending on the operating system) can easily support a Java virtual machine with thousands of threads.
If you need a more accurate answer, it is best to do stress test yourself.
Charlie Martin's answer:
There are many parameters (can be set). For specific virtual machines, they will have their own runtime parameters. (Maximum number of threads) To a certain extent, it is determined by the operating system: What support should the underlying operating system provide to threads? What restrictions are imposed? Does the virtual machine use the thread of the native operating system, or red thread or green thread?
The support provided by the operating system is another issue. If you write a Java program like this:
The code copy is as follows:
class DieLikeADog {
public static void main(String[] argv){
for(;;){
new Thread(new SomeRunaable).start();
}
}
}
(Don't complain about syntax details, this is just beginning) Then of course you want to get hundreds of running threads. However, the cost of creating a thread is relatively high, and the overhead of (too many threads) scheduling becomes prominent. It is still uncertain whether these threads can do useful things.
Upgraded version
OK, can't wait! Here is a small test program that has been slightly polished:
Copy the code as follows: public class DieLikeADog {
private static Object s = new Object();
private static int count = 0;
public static void main(String[] argv){
for(;;){
new Thread(new Runnable(){
public void run(){
synchronized(s){
count += 1;
System.err.println("New thread #"+count);
}
for(;;){
try {
Thread.sleep(1000);
} catch (Exception e){
System.err.println(e);
}
}
}
}).start();
}
}
}
On Intel's OS/X 10.5.6 system, the output of Java 5 is as follows:
The code copy is as follows:
New thread #2547
New thread #2548
New thread #2549
Can't create thread: 5
New thread #2550
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:592)
at DieLikeADog.main(DieLikeADog.java:6)
benjismith's answer:
After reading Charlie Martin's reply, I was wondering if the size of the heap memory can make a difference in the number of threads created, and then I was stunned by the result: on the Vista Home Premium SP1 system, using JDK 1.6.0_11, Set the size of heap memory from 2M to 1024M to execute Charlie's test program. For example: to create 2M heap memory, the virtual machine parameters I use are: -Xms2m -Xmx2m.
Here are my test results:
Copy the code as follows: 2 mb --> 5744 threads
4 mb --> 5743 threads
8 mb --> 5735 threads
12 mb --> 5724 threads
16 mb --> 5712 threads
24 mb --> 5687 threads
32 mb --> 5662 threads
48 mb --> 5610 threads
64 mb --> 5561 threads
96 mb --> 5457 threads
128 mb --> 5357 threads
192 mb --> 5190 threads
256 mb --> 5014 threads
384 mb --> 4606 threads
512 mb --> 4202 threads
768 mb --> 3388 threads
1024 mb --> 2583 threads
So, the size of the heap is really important. However, the heap size and the maximum number of threads are inversely proportional.
This is so weird!
Neil Coffey's answer:
The absolute theoretical maximum number of threads is the process's user address space divided by the size of the thread stack (in reality, if all the memory is used by the thread stack, there will be no program that can run). Therefore, taking the 32-bit Windows system as an example, the user address space of each process is 2G. If the size of each thread stack is 128K, there will be up to 16384 (=2*1024*1024/128) threads. Actually on XP system, I found that about 13,000 threads can be started.
Then, I think your question is essentially: (a) Whether you can effectively manage many threads in your code and not letting them do it is obviously stupid (such as: let them on the same object object (b) Whether the operating system can effectively manage these many threads. Basically, if (a)'s answer is "yes", the answer (b)'s answer is "yes".
Coincidentally, you can set the thread stack size in the Thread constructor, but you don't need to and should not confuse this with the virtual machine parameters.