Mcgoverntheory in StackOverflow raised such a question:
How many threads does a Java virtual machine support? Does it have anything to do with virtual machine developers? With the operating system? Is there any other factor?
Eddie's Answer:
It depends on what CPU, OS, other processes are doing, the version of Java you're using, and other factors. I have seen a Windows server with more than 6,500 threads before downtime. Of course, most threads have nothing to do with it. Once a machine has almost 6,500 threads (in Java), the machine starts to malfunction and becomes unstable.
In my experience, the threads that the JVM holds are positively correlated with the performance of the computer itself.
Of course, you have to have enough native memory, and give Java enough memory, so that each thread can have a stack (virtual machine stack), you can do anything you want to do. Any machine with a modern CPU (AMD or the latest generation of Intel) and 1-2G memory (depending on the operating system) can easily support thousands of threads of Java virtual machines.
If you need a more precise answer, it's best to do the pressure test yourself.
Charlie Martin's Answer:
Here are a lot of parameters (can be set). For a particular virtual machine, it has its own run-time parameters. (Maximum number of threads) partly determined by the operating system: what support does the underlying operating system provide to threads? What restrictions are imposed? is the virtual machine using a native operating system thread or red thread or green thread?
The support provided by the operating system is another issue. If you write to the following Java program:
Copy Code code as follows:
Class Dielikeadog {
public static void Main (string[] argv) {
for (;;) {
New Thread (new somerunaable). Start ();
}
}
}
(Don't complain about grammatical details, this is just the beginning) then you certainly want to get hundreds of threads running. However, the cost of creating a single thread is relatively large, and the overhead of scheduling (over multithreading) becomes prominent. It's not certain that these threads can do useful things.
Upgrade version
All right, I can't wait! Here's a little bit of a small test program that I've added to the point of polishing:
Copy Code 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 from Java 5 is as follows:
Copy Code code 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 wondered whether the heap memory size could be different from the number of threads created, and I was stunned by the result: using JDK on the Vista home Premium SP1 system 1.6.0_ 11, set the heap memory size from 2M to 1024M to execute Charlie's test program. For example: Create 2M heap memory, I use the virtual machine parameter is:-xms2m-xmx2m.
Here's my test results:
Copy Code code as follows:
2 MB--> 5744 threads
4 MB--> 5743 threads
8 MB--> 5735 Threads
MB--> 5724 Threads
MB--> 5712 Threads
MB--> 5687 Threads
MB--> 5662 Threads
MB--> 5610 Threads
MB--> 5561 Threads
MB--> 5457 Threads
128 MB--> 5357 threads
MB--> 5190 Threads
256 MB--> 5014 threads
384 MB--> 4606 Threads
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 maximum number of threads are in an inverse proportional relationship.
This is so weird!
Neil Coffey's Answer:
The absolute theoretical maximum number of threads is the user address space of the process divided by the size of the line stacks (in reality, if the memory is all used to the thread stack, there will be no running program). Therefore, in 32-bit Windows, for example, the user address space for each process is 2G, if the size of each line stacks is 128K, there will be up to 16384 (=2*1024*1024/128) threads. Actually on the 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 let them do what is obviously stupid (e.g., having them wait for the same object and then being called notifyall) ... ), (b) whether the operating system can effectively manage many of these threads. Basically, if the answer to (a) is "yes", the answer to (b) is "yes".
Coincidentally, you can set the size of the line stacks in the thread's constructor, but you don't need to confuse this with the virtual machine parameters.