Kotlin Coroutine Confidence (Pragmatic Bookshelf)

PragmaticBookshelf
Escape callback hell and ship fast, clean code that reads as smoothly as it runs. Squash bugs and stamp out memory leaks with an intuitive concurrency toolkit.

Sam Cooper @sam-cooper

edited by Michael Swaine @michaelswaine

Get the efficiency of asynchronous processing without the compromises. Break free from the outdated trade-offs of callbacks and futures, and enjoy the best of both worlds when you mix suspending functions right into familiar loops and sequential programs. Identify opportunities, anticipate hazards, and master key functions and concepts through real coding projects that put coroutines in context. Unleash the real potential of your code and your hardware by dividing problems into structured tasks that run concurrently. Then sit back and enjoy the confidence that comes from using tried and tested tools and idioms to ship maintainable, fault-tolerant code.

Your code can’t afford to waste resources or block important threads. Whether you’re juggling dozens of inputs and outputs, keeping user interfaces smooth and snappy, or parallelizing heavy workloads, you need a dependable way to write asynchronous code that’s responsive, readable, and reliable. Coroutines combine the benefits of asynchronous execution with the straightforward sequential style of familiar blocking functions, so you can write your code the way you want and let Kotlin take care of running it efficiently.

Build a mental toolkit of coroutine idioms. Following step-by-step examples and real-world explanations, you’ll speed up your code and improve user experiences. Use structured tasks to safely manage errors and resources. Unlock new suspending functions and old control flow possibilities, all while integrating seamlessly with the tools and libraries you already know. Match coroutine tools instinctively to common tasks, and collaborate with confidence when you follow suspending function conventions.

Peek below the surface and see how coroutines keep you safe behind the scenes. Diagnose problems before they happen, and rely on the Three Rules of Jobs to sidestep concurrency pitfalls. Debug and test asynchronous operations, and ship coroutines that make life easy for you, your coworkers, and your users.


Sam Cooper has been working with Kotlin coroutines since their first release, most recently as a tech lead and principal engineer. He has developed software at internationally recognized technology companies, including Amazon Web Services. His writing has been featured in community publications such as Kotlin Weekly, Android Weekly, and Software Testing Weekly.


Don’t forget you can get 35% off with your Devtalk discount! Just use the coupon code “devtalk.com" at checkout :+1:

Latest Threads About This Book Top

Javaru
When I run pets/v23 from page 225, I get a quick flash of the “control panel” window, and then the program exits. There is no output in t...
New
Javaru
For pets/v2/src/main/kotlin/com/example/pets/Helpers.kt (and in subsequent versions), I think it might be helpful to change the block par...
New
Javaru
On page 180 it says: If you’re a fan of fluffy felines, you’ll be starting here. pets/v1/src/main/kotlin/com/example/pets/PetService.k...
New
Javaru
As a minor suggestion, in timers/v19/src/main/kotlin/com/example/timers/TimerApplication.kt, you might want to consider adding // @format...
New
Javaru
On page 133 in the callout text box, it says: Did your IDE warn you about this misplaced Thread.sleep()? … And on page 141 it says: ...
New
Javaru
When I ran the parallel/v8 example on page 125, I did not get the results described of not seeing the timer’s output until the program al...
New
Javaru
For coffee/v3/src/main/kotlin/com/example/coffee/CoffeeApplication.kt, on pg 80, it states that the output will be: Done launching two c...
New
Javaru
The last sentence of the second paragraph in the “What is Structured Concurrency?” text box on page 78 is confusing (highlighting added):...
New
Javaru
I suspect this is something that will be caught in editing, but just in case not… The text box at the bottom of page 78 extends on to the...
New
dhmitchell
In “gallery/v14/src/main/kotlin/com/example/gallery/GetImageFromFile.kt” won’t the .use always call channel.close() on exit? if so, why...
New

Most Active This Week Top

Javaru
I suspect this is something that will be caught in editing, but just in case not… The text box at the bottom of page 78 extends on to the...
New
Javaru
As a minor suggestion, in timers/v19/src/main/kotlin/com/example/timers/TimerApplication.kt, you might want to consider adding // @format...
New
Javaru
For pets/v2/src/main/kotlin/com/example/pets/Helpers.kt (and in subsequent versions), I think it might be helpful to change the block par...
New
Javaru
For coffee/v3/src/main/kotlin/com/example/coffee/CoffeeApplication.kt, on pg 80, it states that the output will be: Done launching two c...
New
Javaru
When I ran the parallel/v8 example on page 125, I did not get the results described of not seeing the timer’s output until the program al...
New
Javaru
The last sentence of the second paragraph in the “What is Structured Concurrency?” text box on page 78 is confusing (highlighting added):...
New
Javaru
On page 180 it says: If you’re a fan of fluffy felines, you’ll be starting here. pets/v1/src/main/kotlin/com/example/pets/PetService.k...
New
Javaru
On page 133 in the callout text box, it says: Did your IDE warn you about this misplaced Thread.sleep()? … And on page 141 it says: ...
New
Javaru
When I run pets/v23 from page 225, I get a quick flash of the “control panel” window, and then the program exits. There is no output in t...
New

Most Active This Month Top

PragmaticBookshelf
Escape callback hell and ship fast, clean code that reads as smoothly as it runs. Squash bugs and stamp out memory leaks with an intuitiv...
New
dhmitchell
The text "“ What we do have is that suspension-point icon next to awaitResponse,” presumes we didn’t have those icons in the execute ver...
New
Javaru
Pg 41 states “Run the code, and you’ll see the placeholder message in a small window, just like last time.” for the code astronomy/v4/src...
New
Javaru
For the example astronomy/v3/src/main/kotlin/com/example/astronomy/AstronomyApplication.kt, despite the statement “That try–catch block i...
New
dhmitchell
In the example “timers/v7/src/main/kotlin/com/example/timers/TimerApplication.kt” Why doesn’t the program just exit after queuing the ti...
New
dhmitchell
Possibly too pedantic, but I’d be tempted to remind the reader that they can certainly put the try/catch inside the launch and it will wo...
New
dhmitchell
To reinforce kotlin patterns, instead of ​val​ astronomyService = Retrofit.Builder()​ .client(createHttpClient().build()) ​...
New
dhmitchell
In “gallery/v14/src/main/kotlin/com/example/gallery/GetImageFromFile.kt” won’t the .use always call channel.close() on exit? if so, why...
New

Most Active This Year Top

Most Active Last Three Years Top

Most Active Over Three Years Top

Get money off!

The Pragmatic Bookshelf

35% off any eBook

Simply use coupon code "devtalk.com" at checkout. See full details here.