4

I'm learning about Multi-threading and concurrency in Java. Read lots of the posts on stack-overflow and online. But not cleared. So please bear with me.

First my doubt is, some people say number of threads can run in concurrently is equivalent to (n= number of processor cores) Is it correct ? I'm really not sure about this statement.

But I have read that we can create as much threads we want in JVM, it only depends upon the memory we have allocated to JVM. Is it correct ?

How should we decide number of threads to run in parallel for one java program ? Is it related to somewhere the processor cores ?

Elena
  • 181
  • 1
  • 11
  • https://stackoverflow.com/a/4759606/2970947 – Elliott Frisch Oct 06 '18 at 06:01
  • @ElliottFrisch, Sir I have seen that post. But am curious to know does it matter to know how many cores am having in processor to run multiple threads in parallel ? or I just need to check JVM memory allocation ? – Elena Oct 06 '18 at 06:03
  • That is the number of threads that can run (in total) on the machine. Note that modern operating systems also [preemptively time slice](https://en.wikipedia.org/wiki/Preemption_(computing)#Time_slice). Java threads are implemented as native threads. – Elliott Frisch Oct 06 '18 at 06:05
  • 1
    You can create as many threads as you wish but one core can only run one thread at a time. – tkausl Oct 06 '18 at 06:05
  • 1
    @tkausl, If only one thread can be executed at a time, then what other threads will be doing that moment ? waiting to be processed by processor ? Then what's the use of multi-threading if threads are like kind of en-queued to be processed by processor ? – Elena Oct 06 '18 at 06:14
  • There is no universal answer here. The exact number of threads that can execute in parallel depends on the exact CPU type, the operating system and even the jvm implementation. Beyond that, that number is often meaningless. A huge server often runs with thousands of threads because most of them are waiting for something to happen. – GhostCat Oct 06 '18 at 06:23
  • @GhostCat, If OS is able to handle multiple applications running in paralle (processes or threads hypothetically)..then why not Java ? – Elena Oct 06 '18 at 06:28

1 Answers1

4

number of threads can run in parallel is equivalent to (n= number of processor cores)

Yes this statement is true.

But I have read that we can create as much threads we want in JVM, it only depends upon the memory we have allocated to JVM. Is it correct ?

Theoretically yes. You can create as many Threads you need in JVM until you get OutOfMemoryError. But creation of Threads is an expensive task so as a best practice consider using a shared pool of threads instead. This can be achieved using ExecutorService framework.

Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
  • You can also run out of stack segment space, the stack segment gets divided among the threads so the more simultaneous threads, smaller the stack of each so it increases the chances of a StackOverflowException (although it's usually pretty big for most applications). – cleberz Oct 06 '18 at 06:12
  • 1
    Most CPUs have hyperthreading these days. 2 or 4, even 8 threads are possible. – GhostCat Oct 06 '18 at 06:19