CodeSmith
CodeSmith

Reputation: 1691

How can I know the maximum number of runnable threads allowed on the current JVM?

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. In particular, I think one of the systems is only allowing a single thread to run so there is no benefit to the multi-threading. What's an easy way to tell when this happens?

ExecutorService exec = Executors.newFixedThreadPool(4);
TestRunnable tr = new TestRunnable();
for ( int i = 0; i < 20; i++ ) {
  try {
     exec.execute(sr);
  } catch (Exception E) {
  }
}

Upvotes: 1

Views: 1266

Answers (1)

Stephen C
Stephen C

Reputation: 718698

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.

Upvotes: 1

Related Questions