非原创. 原文在: https://medium.com/@mohak1712/kotlin-coroutines-thread-sleep-vs-delay-63171fe8a24
What that means is that the Coroutine unblocks the thread that it’s running on while it waits for the result. During that time the thread is free to perform other task like executing another coroutine. As soon as the result is obtained, execution starts from where it was left.
suspend
的意思就是, 协程在执行 suspend 函数的时候, 会释放对他自己所在线程的阻塞. 在suspend 函数执行的同时, 该线程可以去做任何事情. 当 suspend 函数执行完毕之后, 协程就会得到通知然后从 suspend 函数返回的地方继续执行.
From the past few days, I have been trying to understand Coroutines and to be honest I have struggled a lot. For those who don't know about Coroutines, in a nutshell, they are lightweight threads. I am not going to answer questions like what are Coroutines, why Coroutines etc. etc. since there are plenty of good articles related to that out there!. Rather I would be discussing something related to suspending functions.
One of the things I found difficult to wrap my head around are *suspending functions. *So what are they?
Kotlin has a suspend keyword which is its way of telling that this particular function is going to take some time for execution, maybe 10 seconds or even minutes who knows!. When such a function is called from a coroutine, instead of blocking until that function returns like a normal function call, it is suspended. What that means is that the Coroutine unblocks the thread that it’s running on while it waits for the result. During that time the thread is free to perform other task like executing another coroutine. As soon as the result is obtained, execution starts from where it was left.
Note : suspending functions can be called from another suspending functions or coroutines only. In case you try to call it from a normal function you will get an error.