There's no automagic going on with Kotlin coroutines. If you call a blocking function like HttpReader.get()
, the coroutine won't be suspended and instead the call will block. You can easily assure yourself that a given function won't cause the coroutine to suspend: if it's not a suspend
function, it cannot possibly do it, whether or not it's called from a suspend
function.
如果要将现有的阻塞API转换为非阻塞、可挂起的调用,必须将阻塞调用提交给线程池.实现这一目标的最简单方法如下:
val response = withContext(Dispatchers.IO) { HttpReader.get(url) }
withContext
is a suspend fun
that will suspend the coroutine, submit the provided block to another coroutine dispatcher (here IO
) and resume when that block is done and has come up with its result.
您还可以轻松实例化自己的ExecutorService
,并将其用作协同程序调度器:
val myPool = Executors.newCachedThreadPool().asCoroutineDispatcher()
现在你可以写作了
val response = withContext(myPool) { HttpReader.get(url) }
This way you work against a thread pool that is under your control, as opposed to the global IO
. Just keep in mind that you are now also responsible to close()
it.