Do blocking operations in coroutines block the underlying thread?

So I have been struggling with this thought, especially since I was trying to justify the use of reactive APIs with coroutines.

In search of my answer, I’ve read this article by Roman Elizarov, where he gives the example of turning a blocking IO operation into a non-blocking one with the use of

withContext

to run the operation in

Dispatchers.IO

.

Now, this is where I get a bit confused. The

Dispatchers.IO

context is fundamentally an Executor (i.e. a thread pool), correct? So that means that if I have the following code:


suspend fun doesThisBlockAThreadOrWhat() = withContext(Dispatchers.IO) { jpaRepository.findAll() // A typical blocking operation using Spring Data JPA repositories }

it will still block a thread on the underlying

Dispatchers.IO

right? So although we can suspend the execution on the calling side, there is still a thread being blocked that is not re-used to execute any other code while the JPA repository call hasn’t finished.

Is this correct? Am I missing something here?

submitted by /u/hastyyyy
[link] [comments]