2

Q:

1) In a multi-threading application is there a limit to how many threads an application may spawn.

2)If such a limit exists, does the JVM terminate the application and with what error code.

EDIT

3)If an application spawns in very rapid sucsession, would the JVM detect that as a "rough" application

Thanks in advance

Voo
  • 29,040
  • 11
  • 82
  • 156
Aiden Strydom
  • 1,198
  • 2
  • 14
  • 43
  • possible duplicate of [How many threads can a Java VM support?](http://stackoverflow.com/questions/763579/how-many-threads-can-a-java-vm-support) – CharlesB May 22 '12 at 12:00

5 Answers5

6

You should test it for your setup. I did for mine:

import static java.lang.Thread.currentThread;
import java.util.concurrent.CountDownLatch;

public class Test {
  static final Thread t = currentThread();
  public static void main(String[] args) throws Exception {
    int i = 0;
    try {
      while (true) {
        final CountDownLatch l = new CountDownLatch(1);
        new Thread() { public void run() { l.countDown(); park(); }}.start();
        l.await();
        i++;
      }
    } finally { System.out.println("Started " + i + " threads."); }
  }
  private static void park() {
    try { t.join(); } catch (InterruptedException e) {
      System.out.println("Unparked");
    }
  }
}

Output:

Started 2030 threads.
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:658)
    at test.Test.main(Test.java:10)
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • ...and an excellent research about various parameters which influence this limit is here: https://askubuntu.com/a/883677/1067543 – Inego Nov 10 '21 at 05:41
5

No limit specified by the JVM specification. The OS may (probably) limit it as usual per process etc though. Each thread will also allocate a stack etc so a lot of memory could be needed, so depending on the computer what limit is hit first..

Please note: Too many threads are usually inefficient though. Your program could/should probably scale better in another way. Using Thread pools (executor service), asynchronous I/O, fork/join etc depending on your needs.

See also:

Is there any hard limit to a number of threads in Java?

How many threads can a Java VM support?

Community
  • 1
  • 1
Mattias Isegran Bergander
  • 11,811
  • 2
  • 41
  • 49
4

1). No, there no limit. At least not like that. Of course you will run out of memory.

2). OutOfMemoryException.

3). You mean like a Fork bomb? No.

yankee
  • 38,872
  • 15
  • 103
  • 162
2

How many threads may an application generate before beeing terminated by the JVM

The question is based on a false premiss. When the JVM runs out of ability to create new threads it just throws exceptions to the code that tried to create the extra thread. The application isn't 'terminated by the JVM'.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

Comes down to how much you are doing with each individual thread I feel. You can give resources to the JVM with environment variables, so I guess there is a good amount. And the error code will most likely be OutOfMemoryException, Heap Space problems.

It also comes down to, whether you are making manual threads, or using a thread pool. Every time you make a manual thread, it takes a core. A Threadpool does not.

OmniOwl
  • 5,477
  • 17
  • 67
  • 116
  • A thread does not take a core. Any thread may in fact run on a any core each time it is restarted. What you are saying would mean that you can't start more than 2-4 threads. By the time the first line of code in `void main` runs, there's already a dozen threads live in the JVM. – Marko Topolnik May 20 '12 at 18:51
  • Well I meant more that if you start a thread by yourself not using a threadpool, you take up more CPU. – OmniOwl May 20 '12 at 20:06