Статьи

Java: Каково ограничение на количество потоков, которые вы можете создать?

Я видел несколько тестов, в которых у JVM есть потоки 10K. Однако что произойдет, если вы пойдете дальше?

Я рекомендую подумать о том, чтобы иметь больше серверов, как только ваш общий счет достигнет 10 КБ. Вы можете получить приличный сервер за 2 тысячи долларов и мощный сервер за 10 тысяч долларов.

Создание тем становится медленнее

Время создания потока увеличивается с увеличением количества потоков. Для 32-разрядной JVM размер стека ограничивает число потоков, которые вы можете создать. Это может быть связано с ограниченным адресным пространством. В любом случае память, используемая стеком каждого потока, складывается. Если у вас стек 128 КБ, а у вас потоки 20 КБ, он будет использовать 2,5 ГБ виртуальной памяти.

разрядность Размер стека Макс темы
32-битный  64K 32073
32-битный 128K 20549
32-битный 256K 11216
64-битный  64K слишком маленький стек
64-битный 128K 32072
64-битный 512K 32072

Примечание. В последнем случае поток суммирует до 16 ГБ виртуальной памяти.

Java 6, обновление 26, 32-разрядная версия, -XX: ThreadStackSize = 64

4,000 threads: Time to create 4,000 threads was 0.522 seconds 
8,000 threads: Time to create 4,000 threads was 1.281 seconds 
12,000 threads: Time to create 4,000 threads was 1.874 seconds 
16,000 threads: Time to create 4,000 threads was 2.725 seconds 
20,000 threads: Time to create 4,000 threads was 3.333 seconds 
24,000 threads: Time to create 4,000 threads was 4.151 seconds 
28,000 threads: Time to create 4,000 threads was 5.293 seconds 
32,000 threads: Time to create 4,000 threads was 6.636 seconds 
After creating 32,073 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)

Java 6, обновление 26, 32-разрядная версия, -XX: ThreadStackSize = 128

4,000 threads: Time to create 4,000 threads was 0.525 seconds 
8,000 threads: Time to create 4,000 threads was 1.239 seconds 
12,000 threads: Time to create 4,000 threads was 1.902 seconds 
16,000 threads: Time to create 4,000 threads was 2.529 seconds 
20,000 threads: Time to create 4,000 threads was 3.165 seconds 
After creating 20,549 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)

Java 6, обновление 26, 32-разрядная версия, -XX: ThreadStackSize = 128

4,000 threads: Time to create 4,000 threads was 0.526 seconds 
8,000 threads: Time to create 4,000 threads was 1.212 seconds 
After creating 11,216 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)

Java 6, обновление 26, 64-разрядная версия, -XX: ThreadStackSize = 128

4,000 threads: Time to create 4,000 threads was 0.577 seconds 
8,000 threads: Time to create 4,000 threads was 1.292 seconds 
12,000 threads: Time to create 4,000 threads was 1.995 seconds 
16,000 threads: Time to create 4,000 threads was 2.653 seconds 
20,000 threads: Time to create 4,000 threads was 3.456 seconds 
24,000 threads: Time to create 4,000 threads was 4.663 seconds 
28,000 threads: Time to create 4,000 threads was 5.818 seconds 
32,000 threads: Time to create 4,000 threads was 6.792 seconds 
After creating 32,072 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)

Java 6, обновление 26, 64-разрядная версия, -XX: ThreadStackSize = 512

4,000 threads: Time to create 4,000 threads was 0.577 seconds 
8,000 threads: Time to create 4,000 threads was 1.292 seconds 
12,000 threads: Time to create 4,000 threads was 1.995 seconds 
16,000 threads: Time to create 4,000 threads was 2.653 seconds 
20,000 threads: Time to create 4,000 threads was 3.456 seconds 
24,000 threads: Time to create 4,000 threads was 4.663 seconds 
28,000 threads: Time to create 4,000 threads was 5.818 seconds 
32,000 threads: Time to create 4,000 threads was 6.792 seconds 
After creating 32,072 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)

Код

MaxThreadsMain.java

 

От http://vanillajava.blogspot.com/2011/07/java-what-is-limit-to-number-of-threads.html