How many threads can a Java VM support?

前端 未结 12 1122
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 06:32

How many threads can a Java VM support? Does this vary by vendor? by operating system? other factors?

相关标签:
12条回答
  • 2020-11-22 06:56

    Maximum number of threads depends on following things:

  • Hardware Configuration like microprocessor, RAM.
  • Operating System like whether it is 32-bit or 64-bit
  • Code inside the run method. If code inside the run method is huge then single thread object will have more memory requirement
0 讨论(0)
  • 2020-11-22 06:57

    At least on Mac OS X 10.6 32bit, there is a limit (2560) by the operating system. Check this stackoverflow thread.

    0 讨论(0)
  • 2020-11-22 07:04

    I know this question is pretty old but just want to share my findings.

    My laptop is able to handle program which spawns 25,000 threads and all those threads write some data in MySql database at regular interval of 2 seconds.

    I ran this program with 10,000 threads for 30 minutes continuously then also my system was stable and I was able to do other normal operations like browsing, opening, closing other programs, etc.

    With 25,000 threads system slows down but it remains responsive.

    With 50,000 threads system stopped responding instantly and I had to restart my system manually.

    My system details are as follows :

    Processor : Intel core 2 duo 2.13 GHz
    RAM : 4GB
    OS : Windows 7 Home Premium
    JDK Version : 1.6
    

    Before running I set jvm argument -Xmx2048m.

    Hope it helps.

    0 讨论(0)
  • 2020-11-22 07:05

    After playing around with Charlie's DieLikeACode class, it looks like the Java thread stack size is a huge part of how many threads you can create.

    -Xss set java thread stack size

    For example

    java -Xss100k DieLikeADog

    But, Java has the Executor interface. I would use that, you will be able to submit thousands of Runnable tasks, and have the Executor process those tasks with a fixed number of threads.

    0 讨论(0)
  • 2020-11-22 07:07

    This depends on the CPU you're using, on the OS, on what other processes are doing, on what Java release you're using, and other factors. I've seen a Windows server have > 6500 Threads before bringing the machine down. Most of the threads were not doing anything, of course. Once the machine hit around 6500 Threads (in Java), the whole machine started to have problems and become unstable.

    My experience shows that Java (recent versions) can happily consume as many Threads as the computer itself can host without problems.

    Of course, you have to have enough RAM and you have to have started Java with enough memory to do everything that the Threads are doing and to have a stack for each Thread. Any machine with a modern CPU (most recent couple generations of AMD or Intel) and with 1 - 2 Gig of memory (depending on OS) can easily support a JVM with thousands of Threads.

    If you need a more specific answer than this, your best bet is to profile.

    0 讨论(0)
  • 2020-11-22 07:07

    You can process any number of threads; there is no limit. I ran the following code while watching a movie and using NetBeans, and it worked properly/without halting the machine. I think you can keep even more threads than this program does.

    class A extends Thread {
        public void run() {
            System.out.println("**************started***************");
            for(double i = 0.0; i < 500000000000000000.0; i++) {
                System.gc();
                System.out.println(Thread.currentThread().getName());
            }
            System.out.println("************************finished********************************");
        }
    }
    
    public class Manager {
        public static void main(String[] args) {
            for(double j = 0.0; j < 50000000000.0; j++) {
                A a = new A();
                a.start();
            }
        }
    }
    
    0 讨论(0)
  • 提交回复
    热议问题