I used following code to test the max number of thread, but it's very queer.
import java.util.concurrent.atomic.AtomicInteger;
public class TestThread extends Thread {
private static final AtomicInteger count = new AtomicInteger();
public static void main(String[] args) {
try{
while (true) {
(new TestThread()).start();
}
} catch (Error | Exception e) {
System.out.println(e.getMessage());
}
}
@Override
public void run() {
System.out.println(count.incrementAndGet());
while (true)
try {
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
break;
}
}
}
OS is ubuntu:18.04, the memory is 4G, number of processors is 2. JDK: openjdk1.8.0_252
java -Xss1m -Xms1024m -Xmx1023m TestThread
java -Xss512k -Xms1024m -Xmx1023m TestThread
java -Xss1m -Xms2048m -Xmx2048m TestThread
In the result, the number of threads are always equal around 10000th.
- why the result is the same.
- I set the -Xss1m in the command, does it mean each thread will have alone 1m memory? if it has alone memory, 10000th threads will have 10G memory, actually there just 4G memeory in the machine.