I have a multi-threaded application that runs in several different environments and I think that sometimes the environment limits the total number of threads that can be run at the same time.
Yea, the environment does limit the number of threads that can actually run at the same time. Specifically:
- The hardware cannot run more threads simultaneously than there are processors (or hyperthreads) available on the physical machine or virtual machine.
Various OS limits may prevent you using all available resources; e.g. ulimit and cgroup based limits.
If the system is busy, your JVM may be competing with other application processes.
There may be concurrency bottlenecks in your Java code; e.g. points where one thread's locking of some data structure is frequently holding up other threads.
However, the JVM itself is not going to artificially limit you.
You can find out the notional number of available processors using the Runtime.availableProcessors
method.
Note: the number of "runnable" threads is NOT the same thing as the number of "running" threads; i.e. the number of threads that can be physically executing instructions at the same time. The number of "runnable" threads is the number of threads that could be scheduled for execution ... if there were enough processors available.