简体   繁体   中英

What is limiting the number of threads created in a Java application?

The class below seems to consistently report 2542 threads being the maximum possible, irrespective of memory allocated to the app. What is limiting the number of threads here? Memory, CPU, internal JVM setting?

Thanks

public static void main(String... args) {

    int taskNumber = 0;
    while (true) {
        try {
            new MyThread().start();
        } catch (OutOfMemoryError e) {
            System.out.println("Thread " + taskNumber+" could not be created.");
            System.exit(0);
        }
        taskNumber++;
    }
}

Your opperating system has a limit of how many threads it can handle.

To get the number, execute the appropriate command on your machine:

Linux: sysctl kernel.threads-max

Mac OSX: sysctl kern.num_threads

ADDITION : Maybe you can find some interesting answers in this thread: How many threads can a Java VM support?

For native threads the limit is in the underlying operating system resource allocation for the JVM process.

For green threads the limit is in the JVM implementation, but it runs on a single native thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM