What is the difference between suspending vs. blocking?
What is the difference between suspending vs. blocking?
The main difference between suspending and blocking in Kotlin coroutines is:
Suspending a coroutine allows it to pause execution at certain points without blocking a thread, enabling other coroutines to run on the same thread[1][2]. Suspending is accomplished using the suspend
keyword on functions that can pause coroutines, like delay()
or withContext()
[2].
Blocking a thread, on the other hand, halts execution and prevents other work from being done on that thread until the blocking operation completes[1]. Blocking can occur when using regular Java I/O methods or Thread.sleep()
inside a coroutine[1][2].
When a coroutine suspends, it saves its state and local variables, allowing the thread to be returned to the pool to run other coroutines[4]. The coroutine can then be resumed later on ...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào