Câu hỏi phỏng vấn Kotlin
Câu hỏi

What is the difference between suspending vs. blocking?

Câu trả lời

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

middle

Gợi ý câu hỏi phỏng vấn

junior

What is the difference between var and val in Kotlin?

middle

What is lateinit in Kotlin and when would you use it?

middle

What is the purpose of Unit-returning in functions? Why is VALUE there? What is this V ALUE ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào