Posts

Suspend Is Not Async: Why Kotlin suspend Doesn't Switch Threads

Image
 Understanding the difference between execution context and the permission to pause. There is a recurring “ghost” in Android and KMP production code: the  Blocking Suspend Function . Most developers believe that marking a function with  suspend  magically teleports the execution to a background thread. This misunderstanding is the #1 cause of "mysterious" UI stutters in coroutine-based apps. To master coroutines, we must dismantle the myth and look at what the Kotlin compiler is actually doing under the hood. 1. The Visual Mental Model: Thread vs. Coroutine vs. Suspension To understand high-performance Kotlin, you must differentiate between the  Worker , the  Task , and the  Pause . Thread (The Worker):  An OS-level resource. Expensive to create (~1MB stack) and expensive to switch. Coroutine (The Task):  A lightweight unit of work scheduled by a dispatcher. You can have thousands (even millions, memory permitting) because they are just objec...

Kotlin Coroutines: launch vs async – Why One Returns Nothing and the Other Returns Pain

Image
 Mastering Exception Ownership and Structured Concurrency to Eliminate Silent Production Bugs Kotlin Coroutines are built on the bedrock of structured concurrency, but misunderstanding the contract between  launch  and  async  is one of the fastest ways to introduce hidden production bugs. Most developers learn that the difference is simply about whether you need a return value, but the reality is deeper:  it’s about how your system reacts to failure. To write senior-level code, you must shift your mental model from  Syntax  to  Exception Ownership . 1. Kotlin Coroutine launch vs async: Fail-Fast vs. Deferred Failure Understanding the return types is the first step toward mastering structured concurrency. launch  — The Fast-Fail Guardian Goal:  Side-effects (UI updates, DB writes, Analytics). Return:   Job . launch  follows the  Fail-Fast  principle. In a standard  CoroutineScope , if a  launch  bl...