Ashok Koyi
Ashok Koyi

Reputation: 5387

Does CPU waste time on a java thread thats blocked for IO read/write?

If a thread is blocked while writing data to IO, does the CPU need to give this thread any time till this IO operation is complete?

If so, why should the CPU relinquish?

If not, is there any thing otherthan "stack per thread" that makes "request per thread" not to perform as good as "non blocking IO with shared threads" under heavy load?

PS: I have read a decent number of SO questions on this topic & I could not find an answer that addressed this specific aspect

Upvotes: 6

Views: 1593

Answers (1)

David Haim
David Haim

Reputation: 26476

If a thread is blocked while writing data to IO, does the CPU need to give this thread any time till this IO operation is complete?

No, the Operating system will just dequeue a thread that waits to run again and resume it.

If not, is there any thing otherthan "stack per thread" that makes "request per thread" not to perform as good as "non blocking IO with shared threads" under heavy load?

Dequeuing a waiting thread and resuming it may be cheap for one thread, but it's not cheap when you have thousands of them. don't forget the operating system has to calculate which thread to resume (according to priority), where to resume it (according to the available CPUs and affinity) and the CPU itself would probably have to load the memory used by the thread into the cache lines, which is a real bitch.

Not to mention that the thread that goes to sleep has to flush its data into the RAM itself from the cache lines, and it's a very expensive operation (the cache lines are there for a reason, a big one).

And yeah, the memory consumed by thousands of threads can take up significant amount of memory which slows down the entire system.

Now, it's not like an HTTP server can't perform well with only threads and blocking IO, but since it's so easy today to use asynchronous operations (using futures, async/await, callbacks), that we simply prefer asynchronous IO for servers that actually need speed.

Upvotes: 3

Related Questions