Edit:
As what @Petesh said, I reached the kern.num_taskthreads
limit rather than the overall thread limit, which limits the number of threads for an individual process.
The sysctl kern.num_taskthreads
is:
kern.num_taskthreads: 2048
And when I used the VM args, -XX:ThreadStackSize=1g
, I could only create 122 threads; with -XX:ThreadStackSize=2g
, 58 threads was created. It's reasonable.
But it's still strange that no matter how I changed the -Xss
args, the result is always 2031. The -Xss
args seems only works for main thread which I'm not sure for now.
Original question:
I ran a test to find out how many threads that one JVM can create. And when I adjusted the JVM args, -Xmx
and -Xss
, the result didn't change.
Here is the code:
public class ThreadTest {
public static void main(String[] args) {
int count = 0;
try {
while (true) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(360);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
System.out.println(count);
}
} catch (Error e) {
e.printStackTrace();
}
}
}
And the OS info:
- Model Name: MacBook Pro
- Model Identifier: MacBookPro11,4
- Processor Name: Intel Core i7
- Processor Speed: 2.2 GHz
- Number of Processors: 1
- Total Number of Cores: 4
- L2 Cache (per Core): 256 KB
- L3 Cache: 6 MB
- Memory: 16 GB
The java version:
➤ java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Dynamic Code Evolution 64-Bit Server VM (build 25.71-b01-dcevmlight-1, mixed mode)
The sysctl kern.num_threads
:
kern.num_threads: 10240