How many threads can a Java VM support?

前端 未结 12 1109
爱一瞬间的悲伤
爱一瞬间的悲伤 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 07:10

    The absolute theoretical maximum is generally a process's user address space divided by the thread stack size (though in reality, if all your memory is reserved for thread stacks, you won't have a working program...).

    So under 32-bit Windows, for example, where each process has a user address space of 2GB, giving each thread a 128K stack size, you'd expect an absolute maximum of 16384 threads (=2*1024*1024 / 128). In practice, I find I can start up about 13,000 under XP.

    Then, I think you're essentially into whether (a) you can manage juggling that many threads in your code and not do obviously silly things (such as making them all wait on the same object then calling notifyAll()...), and (b) whether the operating system can. In principle, the answer to (b) is "yes" if the answer to (a) is also "yes".

    Incidentally, you can specify the stack size in the constructor of the Thread; you don't need to (and probably shouldn't) mess about with VM parameters for this.

提交回复
热议问题