Java threads stress test
concurrency, java, multithreading
Solution
Counting from 0 to 100000 without doing anything else is so fast (and is even probably removed completely by Hotspot, reducing the `run()` method to a noop) that you'll never have a large number of concurrent threads: it takes more time to start a new thread than it takes for threads to complete and die.
Why don't you make all your threads sleep forever? That would guarantee that they're all started.
Problem
I am trying to run a small test on the maximum number of concurrent threads I could run on a single JVM and the time it takes to create a large number of threads. I have the following trivial code ``` public class Threading { public static void main(String[] args) { Runnable task = new Runnable() { @Override public void run() { for (int i = 0; i < 100000; i++) ; } }; long start = System.nanoTime(); int runs = 1000000; for (int i = 0; i < runs; i++) new Thread(task).start(); long time = System.nanoTime() - start; System.out.printf("Time for task to complete: %.2f seconds", (double) time / 1000000000.0); } } ``` I am using VisualVM to track the number of active threads. The results I am getting seem odd. The peak active threads I am reaching is around 100 and the average number of active threads is around 15. and It's taking more than 60 seconds for the 1 million threads to be created. Am I doing anything wrong here?