0

I am new to Java and not familiar with computer science / OS. I have question related to Java multi-threading and CPU .

As per my understanding, Java allows us to create multiple threads with can run in parallel at the same time. So if need to execute say 2 threads at the same time do I need to have 2 CPU present on machine. So my confusion is that I need to execute say 8 threads at the same time do I need to have 8 CPU ?

Please help me understanding this concept.

Thanks

  • The short answer is no, you do not need 1 CPU per thread. – D.B. Jan 10 '17 at 17:30
  • You might also want to talk to a prof about this as your question represents a fundamental misunderstanding of threads. – Pete B. Jan 10 '17 at 17:31
  • To have something truly run at the same time it needs to run on a separate core. So if you have a 4 core cpu, it can run 4 threads at the same time. However, threads can be interleaved on a single core. So if one thread needs to wait for a short/long time, another thread can use that core while it waits. So if the operation you want to perform doesn't require any waiting, then you wont see any performance increase by having more than a single thread per core. – flakes Jan 10 '17 at 17:34
  • In order to run a multi-threaded application, you don't need multiple CPUs. A single CPU can still run multiple threads as if they are multiple processes - only difference will be that two threads of the same process would share the same virtual memory space. However if you have multiple CPUs or a CPU with multiple cores, you will gain real advantage of your multithreaded application because each core then may execute a thread in parallel. – VHS Jan 10 '17 at 17:40

0 Answers0