Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is having many threads in a JVM application expensive?

I'm currently learning about actors in Scala. The book recommends using the react method instead of receive, because it allows the system to use less threads.

I have read why creating a thread is expensive. But what are the reasons that, once you have the threads (which should hold for the actor system in Scala after the initialization), having them around is expensive?

Is it mainly the memory consumption? Or are there any other reasons?

like image 918
rolve Avatar asked May 08 '12 13:05

rolve


2 Answers

Using many threads may be more expensive than you would expect because:

  • each thread consumes memory outside of heap which places restriction on how many threads can be created at all for JVM;
  • switch from one thread to another consumes some CPU time, so if you have activity which can be performed in a single thread, you will save CPU cycles;
  • there's JVM scheduler which has more work to do if there are more threads. Same applies to underlying OS scheduler;
  • at last, it makes little sense to use more threads than you have CPU cores for CPU-bound tasks and it makes little sense to use more I/O threads than you have I/O activities (e.g., network clients).
like image 178
Victor Sorokin Avatar answered Oct 11 '22 13:10

Victor Sorokin


Besides the memory overhead of having a thread around (which may or may not be small), having more threads around will also usually mean that the schedule will have more elements to consider when the time comes for it to pick which thread will get the CPU next.

Some Operating Systems / JVMs may also have constraints on the amount of threads that can concurrently exist.

Eventually, it's an accumulation of small overheads that can eventually account to a lot. And none of this is actually specific to Java.

like image 26
Romain Avatar answered Oct 11 '22 12:10

Romain