Notes on a large number of Java threads
-Xms64M-Xmx512M-Xss200K-Xms64M: sets the JVM initial memory to 64 M. -Xmx512M: sets the JVM maximum available memory to 512 MB. -Xss200K: set the size of a single thread to 200 kb. In the program-the Xss setting is too small, and the program may report an error. The default value is 512 KB. However, if you need a large number of threads, you can reduce-Xss to get more threads.
Package com. competition. score. test; import java. util. concurrent. countDownLatch; public class TestThread {public static void main (String [] args) {for (int I = 0; I ++) {System. out. println ("I =" + I); new Thread (new HoldThread ()). start () ;}} class HoldThread extends Thread {CountDownLatch cdl = new CountDownLatch (1); public HoldThread () {this. setDaemon (true);} public void run () {try {cdl. await ();} catch (InterruptedException e ){}}}
The maximum number of threads available for testing with the above program. Note: after running the preceding program, restart the virtual machine to clear the thread usage.