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 basic difference between fold and reduce in Kotlin? When to use which?

middle

What is the Kotlin double-bang !! operator?

middle

How would you create a singleton with parameter in Kotlin?

Bình luận

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

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