Java Multithreading – Computation vs IO Intensive Operations

javamultithreading

I came across below statement at this blog

Computation intensive operations should use a number of threads lower than or equal to the number of cores, while IO intensive operations like copying files have no use for the CPU and can therefore use a higher number of threads

I am not getting the logic/reasoning behind why number of threads can be greater than cores IO intensive operation while should be lower in case of
Computation intensive operations?

Best Answer

Threads compete for some resource. If more threads want the resource than you have resources available, they are just blocking each other and you have overhead from task switching. The question is, what kind of resource is involved here?

For CPU-bound threads the resource is usually one CPU core. In some cases cores share some resource, e.g. caches or FPUs. In particular Intel's Hyperthreading often doesn't help with CPU-bound threads. So you might stop seeing speedups long before you are “using” all your cores.

For I/O-bound threads, the question is what precise kind of I/O is involved here. If the contested resource is a hard disk drive, then more than one concurrent access will slow things down. If the contested resource is a network interface, then any number of threads can access it without any problems as long as the total bandwidth of the interface is not exceeded.

For I/O bound threads the number of CPU cores is completely irrelevant. In fact you don't need multiple threads to do parallel I/O: asynchronous I/O and event-based systems are entirely sufficient here.