Java の上限スレッド数
環境依存かもしれないけれど,適当にコードを書いて実験してみた.
ThreadTest.java
public class ThreadTest { public static void main(String[] args) { int count = 0; try { while ( true ) { new NeverEndingThread().start(); count++; } } catch ( OutOfMemoryError e ) { e.printStackTrace(); System.err.println("number of threads = " + count); System.exit(1); } } } class NeverEndingThread extends Thread { public void run() { try { while( true ) { sleep(1000); } } catch ( Exception e ) { e.printStackTrace(); System.exit(1); } } }
Fedora Core 3 (kernel 2.6.9-1.667smp)
Pentium 4 3.4F GHz (Hyper-Threading on)
Mem 2GB + Swap 2GB
Java 1.4.2_08-b03
$ java ThreadTest java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start(Native Method) at ThreadTest.main(ThreadTest.java:7) number of threads = 7619
※何回かの試行で変動するが,大体 7600 強.
Windows XP SP2
Pentium M 1.1GHz
実メモリ 760MB + 仮想メモリ 1137MB
Java 1.5.0_04
C:\>java ThreadTest java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Unknown Source) at ThreadTest.main(ThreadTest.java:7) number of threads = 7117