Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is multithreading actually parallel processing or just an illusion of one?

Suppose there are 10 disjoint and independent tasks. If single threaded execution takes 10 minutes, each task taking 1 minute, then will 10 threads, one for each task complete the execution in 1 minute?

Since only a single instruction can be ran at a time and assuming that CPU is only running this process. Isn't multithreading just a very fast context switching between the threads, giving an illusion of parallel processing, but not actually being processing?

So, since the number of instructions to be executed are same by the CPU, whether it be single threaded or multi, shouldn't it take same time for completion? If yes, isn't stating that multithreading is a means of parallel processing false?

like image 810
Meet Avatar asked Jan 28 '23 07:01

Meet


2 Answers

Java multi-threading typically offers real parallelism ... not just an illusion of parallelism.

Suppose there are 10 disjoint and independent tasks. If single threaded execution takes 10 minutes, each task taking 1 minute, then will 10 threads, one for each task complete the execution in 1 minute?

Possibly yes, possibly no. It depends on a couple of things:

  1. Are the tasks really disjoint? For example, if they involve shared data structures and synchronization they are not disjoint.

  2. Are there (at least) 10 physical cores available to run the 10 threads?

  3. Is there sufficient memory bandwidth for the 10 threads to not to be bottlenecked on reads / writes to RAM?

Since only a single instruction can be ran at a time ...

Actually, this is not technically correct. Even with a single core, a CPU can be executing multiple instructions at the same time due to pipelining.

But we can say that a given core or hyperthread on a conventional machine will typically only execute one instruction stream at a time.

... assuming that CPU is only running this process.

I assume you mean not the GPU1, and a CPU with only one core and no hyperthreading.

Those are not a valid assumptions even for low-end laptops / desktops these days.

Isn't multithreading just a very fast context switching between the threads, giving an illusion of parallel processing, but not actually being processing?

Actually, if you are running multiple threads on one core, then the context switches between threads take a long time (hundreds of h/w instructions). And they occur infrequently.

But if you assume that there is only one core / hyperthread, then you are correct that threads cannot execute in parallel.

So, since the number of instructions to be executed are same by the CPU, whether it be single threaded or multi, shouldn't it take same time for completion? If yes, isn't stating that multithreading is a means of parallel processing false?

If you assume that there is only one core / hyperthread available, then your conclusion correct. However, that assumption is typically NOT correct on modern computers.

For example my 3 year old Dell laptop has a CPU with 2 cores and 2 hyperthreads per core, which could give a theoretical speedup (due to hardware parallelism!!) of up 4 times. (And that's not considering the possibility of using the GPU ...)

Note that typical server-grade machines have 2 or 4 CPUs per blade, and possibly more. And some server-grade CPUs go to 16 cores or more. So it is not uncommon for a single computer to have 64 cores.


1 - Normal Java won't use the GPU for general computation, so this is a bit of a red herring.

like image 134
Stephen C Avatar answered Feb 07 '23 17:02

Stephen C


Yes, Java is inherently multi-threaded. A single Java program can have many different threads executing independently and continuously. Three Java applets on the same page can run together with each getting equal time from the CPU with very little extra effort on the part of the programmer.

This makes Java very responsive to user input. It also helps to contribute to Java's robustness and provides a mechanism whereby the Java environment can ensure that a malicious applet doesn't steal all of the host's CPU cycles.

Unfortunately multithreading is so tightly integrated with Java, that it makes Java rather difficult to port to architectures like Windows 3.1 or the PowerMac that don't natively support preemptive multi-threading.

There is a cost associated with multi-threading. Multi-threading is to Java what pointer arithmetic is to C, that is, a source of devilishly hard to find bugs. Nonetheless, in simple programs it's possible to leave multi-threading alone and normally be OK

like image 35
Basil Battikhi Avatar answered Feb 07 '23 19:02

Basil Battikhi