Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
【One-Line Pitch】
A practical, example-driven guide to Kotlin coroutines that takes you from "what is a coroutine?" to confidently structuring concurrent work with builders, jobs, flows, and channels. Best for Kotlin developers (especially Android) who want to write correct asynchronous code without callback hell.
【Book Arc】
- **Opening (~0%–10%)**: Frames the problem — asynchronous work and "callback hell" — and introduces coroutines as the solution, including the basic vocabulary of scope, builder, dispatcher, and suspend function.
- **Early (~10%–32%)**: Covers the core building blocks: coroutine builders (`launch()`, `async()`), dispatchers and their defaults, `CoroutineScope` (including why `GlobalScope` is a code smell), and the fundamentals of `Job` management.
- **Middle (~32%–55%)**: Deepens job control — lazy start, job states, cancellation (including cooperative cancellation, `cancelAndJoin()`, `NonCancellable`, and yielding), plus deferring results with `Deferred`.
- **Late (~55%–80%)**: Moves into data streams: `Flow` (creation, consumption, dispatchers, operators, exceptions, `retry()`/`retryWhen()`), channels (send/receive, capacity, broadcasting), and `StateFlow`.
- **Ending (~80%–100%)**: Bridges coroutines to existing callback-based APIs and shows how to create custom scopes, tying the pieces together for real-world integration.
【Key Takeaways】
- **Coroutines solve callback hell by letting you write sequential-looking async code** (Opening): suspend functions pause without blocking threads, so the same code reads top-to-bottom while running concurrently.
- **Builders define intent: `launch()` for fire-and-forget, `async()` for results** (Early): `launch()` returns a `Job` for management; `async()` returns a `Deferred` whose `await()` yields the value.
- **Dispatchers decide where code runs, and defaults are not always obvious** (Early): `Dispatchers.Default` is the usual default, but scopes like Android's `viewModelScope` use `Dispatchers.Main` — declaring your dispatcher explicitly is safest and most self-documenting.
- **`GlobalScope` is a code smell in production** (Early): it lives for the whole process and is easy to misuse; prefer a focused `CoroutineScope` tied to a component's lifecycle.
- **Cancellation is cooperative, not automatic** (Middle): a canceled job only stops when code checks in (e.g., `delay()`, `yield()`, or `withContext`); blocking loops like a busy-wait ignore cancellation entirely.
- **`NonCancellable` lets cleanup finish after cancellation** (Middle): wrapping a `finally` block in `withContext(NonCancellable)` ensures essential cleanup runs even when the job is canceled.
- **Flows are cold streams; channels are hot** (Late): `Flow` emits on collection and supports operators like `retry()`/`retryWhen()` for transient failures; channels support send/receive with capacity planning and broadcasting.
- **`StateFlow` is the modern state-holder** (Late): it replaces `ConflatedBroadcastChannel` and compares favorably to `LiveData`, though it has its own limitations worth understanding.
【Reading Tips】
- **Deep-read the builders, jobs, and cancellation chapters** (Early–Middle): these are the foundation; skimming them will make later flow/channel material harder to follow.
- **Skim the introductory callback-hell framing** (Opening) if you already know why async is hard — get to the coroutine mechanics quickly.
- **Run the Klassbook samples as you go**: the book leans on executable snippets, and seeing output (especially for concurrency ordering) cements the mental model.
- **Pay special attention to cancellation and dispatcher sections**: these are the most common sources of production bugs and the hardest spots to get right.
- **Treat flows, channels, and `StateFlow` as a toolkit to compare**, not memorize — note when each is appropriate rather than learning every operator.
【Coverage Limits】
This guide is based on stratified excerpts covering the table of contents, preface, and selected chapters; some chapters (e.g., deferring results, structuring concurrency, custom scopes) are only partially represented, so specific details there may be understated.
Page 5
........................................................ 95 ◦ Using a SendChannel ................................................................ 99 ◦ Consu...
call, it knows that it can suspend execution of that first coroutine, if desired. In the sequential example, we only have one coroutine, so Kotlin just waits...
delay(2000L) } } (from "Lazy Coroutines" in the Klassbook) If you run this sample, you will not see “This is executed after the delay”, because that coroutin...
hing) • Returns the emitted object for a single-object Flow • Throws an exception if the Flow does not close after emitting its first object However, singleO...
t means it is always safe to get the value from a StateFlow. value can be of type T, not T?, which would force you into a never- ending set of safe calls or...
tor = AsyncTask.THREAD_POOL_EXECUTOR, post: (T) -> Unit 166 APPLYING COROUTINES TO YOUR UI val onChange = object : View.OnLayoutChangeListener { override fun...
y the main code of the motor. The “content” for the Content state consists of a list of RowState objects, where each RowState is a formatted rendition of the...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
Elements of Kotlin Coroutines Version 0.2 (Mark L. Murphy)(Z-Library)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share to WeChat Moments
Elements of Kotlin Coroutines Version 0.2 (Mark L. Murphy)(Z-Library)
Scan the QR code with WeChat to open this book
Tap the ··· menu in the top-right corner
Choose Share to Moments
You are already in WeChat. Tap ··· in the top-right corner and choose Share to Moments.
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
Elements of Kotlin Coroutines Version 0.2 (Mark L. Murphy)(Z-Library)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
这本书围绕 Kotlin 协程展开,作者 Mark L. Murphy 试图把协程从概念到落地使用的关键环节讲清楚。从目录线索看,书中专门讨论了挂起函数的使用准则,其中一条是“在需要的地方挂起”,说明作者关注的不只是语法本身,更是协程在真实代码中该如何组织才合理。书中也触及协程与线程的关系问题:协程究竟是真的跑在各自的操作系统线程上,还是像 Java 绿线程或 Windows fiber 那样被换来换去,这个疑问被提出来,意味着读者会接触到协程调度机制的底层讨论,而不是停留在 API 调用层面。
另一条线索是关于等待与延时的做法。摘录提到用忙循环一直转到期望的时间过去,并明确指出这在几乎所有平台和语言上都不是好主意,更不用说跑在浏览器里的 Kotlin/JS 了。这提示书中会辨析哪些看似能用的写法其实应当避免,帮助读者建立对协程中时间控制手段的正确判断。
状态管理也是书里着墨的部分。关于 StateFlow 的说明指出,从它取 value 总是安全的,value 的类型是 T 而不是 T?,因此不必陷入没完没了的空安全调用或额外判空;一旦开始收集,也有相应的行为需要理解。这部分内容把协程与响应式数...
Digital LibraryAI Assistant
Hi — I can help you pick a book. Tell me a topic, language, or level, e.g. “a light English read”. If that's too broad, I'll ask one follow-up.
Loading comments...
Reply to Comment
Edit Comment