1

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) {
  }
}
CodeSmith
  • 1,621
  • 1
  • 13
  • 31
  • http://stackoverflow.com/questions/763579/how-many-threads-can-a-java-vm-support – Alex Coleman Aug 07 '12 at 00:15
  • It is more of OS capability than Java. – kosa Aug 07 '12 at 00:18
  • @thinksteep Yeah, thats what it said on the link I gave him – Alex Coleman Aug 07 '12 at 00:37
  • I am more interested in the situation where JVM is somehow restricting the maximum number to something arbitrarily low, like 1 or 2, than the absolution OS maximum. – CodeSmith Aug 07 '12 at 02:02
  • @CodeSmith - I'm not aware that the JVM will do that. Do you have any evidence that it does, or is this just a "what if" question? – Stephen C Aug 07 '12 at 02:28
  • I'm sure that something is restricting my program but it may not be the JVM. On my test system with eight processors it runs very quickly but on the production system it runs very slowly/ Like the same speed as if I didn't using threads and the run speed is comparable to my test system running in single thread. So, test system goes fast with threading and production system has no change. I don't have access to the production system to figure out anything directly. – CodeSmith Aug 07 '12 at 06:44
  • But that is not evidence of restricting the number of RUNNABLE threads. – Stephen C Aug 07 '12 at 07:12
  • It may just be a concurrency bottleneck as suggested below. Thank you for the ideas all. – CodeSmith Aug 07 '12 at 07:27

1 Answers1

1

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216