Is there a method or utility to determine how many threads can be created in a program, for example with Executors.newFixedThreadPool(numberThreads)
? The following custom rollout works but is obviously not an enterprise grade solution:
public class ThreadCounter {
public static void main(String[] args) {
System.out.println("max number threads = " + getMaxNumberThreads());
}
static int getMaxNumberThreads() {
final int[] maxNumberThreads = {0};
try {
while (true) {
new Thread(() -> {
try {
maxNumberThreads[0]++;
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
}
}).start();
}
} catch (Throwable t) {
}
return maxNumberThreads[0];
}
}