Difference between runBlocking and async

I have a blocking call into an external library. I would like to have these calls run into a special pool, but I would like the calling coroutine to park - not block - while it’s waiting for the result computed on the pool.

I create a pool:

private val tpfContext = newFixedThreadPoolContext(4, "tpf-pool")

And then - by printing the theread name - I seem to get the same results, whether I do:

runBlocking(tpfContext) { ... }

or

async(tpfContext) { ... } .await()

Are they the same thing? because the docs for runBlocking specify that the thread is blocked, but what happens if you specify a pool? which thread is blocked? Idea suggests that runBlocking is the wrong construct.

runBlocking will block the calling thread while it waits for your job to finish – never call this in a coroutine.

async is better, but an exception thrown in your job could end up cancelling the parent job, which you probably don’t want.

You should use withContext(tpfContext) {...}, which is designed for this purpose.

Thanks, that’s what Idea ended up suggesting :slight_smile: