Java虚拟机最多支持多少个线程?
源 / sina 作者 / miracle1919
class DieLikeADog {public static void main(String[] argv){for(;;){new Thread(new SomeRunaable).start();}}}
public class DieLikeADog {private static Object s = new Object();private static int count = 0;public static void main(String[] argv){for(;;){new Thread(new Runnable(){public void run(){synchronized(s){count += 1;System.err.println("New thread #"+count); }for(;;){try {Thread.sleep(1000); }catch (Exception e){System.err.println(e); } } } }).start(); } }}
New thread #2547New thread #2548New thread #2549Can't create thread: 5New thread #2550Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native threadat java.lang.Thread.start0(Native Method)at java.lang.Thread.start(Thread.java:592)at DieLikeADog.main(DieLikeADog.java:6)
2 mb --> 5744 threads4 mb --> 5743 threads8 mb --> 5735 threads12 mb --> 5724 threads16 mb --> 5712 threads24 mb --> 5687 threads32 mb --> 5662 threads48 mb --> 5610 threads64 mb --> 5561 threads96 mb --> 5457 threads128 mb --> 5357 threads192 mb --> 5190 threads256 mb --> 5014 threads384 mb --> 4606 threads512 mb --> 4202 threads768 mb --> 3388 threads1024 mb --> 2583 threads
-END-
【读者福利】9.9元Python编程课
专为零基础小白打造
学Python就像打游戏
⏰限时24h,速速抢购👇
