Kotlin Coroutine Confidence (PragProg)

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:

3 Likes

Congrats to author @sam-cooper on the beta!

2 Likes

Hello Sam,

I bought the Beta and today I tried out the frist code example, the one page 16 with the Swing Window.

In the book you describe, that the message “Please wait for 10 seconds…” does not appear and that the window can’t be resized or closed during the 10 seconds that the Thread is sleeping. However, on my system, the message appear and the window can be resized and closed. After 10 seconds “Done!” appears. Window stays reactive.

One might say, isn’t it a good thing, that the window does not freeze. But obviously, you wanted to make a point here about blocking the UI thread. Any idea why I can’t observe the behaviour you have described?

1 Like

Ah, my bad. I overlooked the “SwingUtilities.invokeLater {}”. Now it’s behaving as described. Sorry, haven’t used Swing for years.

1 Like

Thank you for mentioning it anyway! If you missed it, I bet other people will too. Perhaps there’s something I can do to make that code example clearer. I’ll have a look.

And welcome aboard! It’s great to have you as a Beta reader.

1 Like

Hi! A huge hello and welcome to everyone joining me on this Beta journey and beyond. It’s great to be here!

I’m Sam, the author of Kotlin Coroutine Confidence. You might know me from my Medium articles, my StackOverflow contributions, or the Kotlin Slack community.

I’ve been lucky enough to spend many years programming in Kotlin, with and without coroutines. I’ve also worked on major projects in Java, Python, C#, and JavaScript, so I know what I’m comparing it to! Kotlin’s unique approach to asynchronous programming is a breath of fresh air, but that can also make it unfamiliar. I’m so excited to share some hands-on guidance that will help make coroutines easy and accessible for even more people!

I’m looking forward to hearing your feedback, and I hope you have as much fun reading the book as I’ve had writing it.

1 Like

Hi Sam @sam-cooper,

I have what I hope is a fun challenge for you. When working on Modern Asynchronous JavaScript, we wanted to come up with a simple concrete analogy for asynchronous programming. It turns out that task wasn’t as easy as we thought it would be. If you had to describe something we all do in real life that mimics asynchronous code, what would that be?

Thanks,
Margaret

1 Like

Thanks for the challenge, Margaret! That’s such a great question. Kotlin’s suspend keyword can make asynchronous operations look and feel just like any other code, so a lot of the analogies that might have worked for old-style asynchronous callbacks don’t quite click with coroutines. I bet you found the same thing with JavaScript’s async/await keywords in Modern Asynchronous JavaScript.

The analogy I use to introduce Kotlin Coroutine Confidence is a half-finished jigsaw puzzle taking up space on a table. When you want to use the table for something else, how do you move the puzzle to one side without having it fall to pieces? One way is to always assemble your jigsaw puzzles on top of a big ol’ tray. This is how you turn the puzzle into an asynchronous task—one which you can pause and put to one side when you’re not working on it, freeing up the table to be used for other activities.

I think this story is a really fun way to explain what asynchronous execution means in a modern language like Kotlin or JavaScript. I hope readers enjoy it too!

1 Like

Beta 2.0 is ready!

Here’s what’s new:

  • Explore a whole new chapter on unit testing with suspending functions and coroutines. Wrap up the journey, consolidate everything you’ve learned, and get the final piece of coroutine confidence. No software is complete without tests!
  • Get to grips with the UI thread quicker in the very first code example. You’ll learn what that SwingUtilities.invokeLater() function is doing there right away, so you can move past it and get to the coroutines even faster. Thanks @Lew for the valuable feedback on this section!
  • Grasp complex concepts faster with some minor updates to the style and of consistency of illustrations and diagrams.

Check your inbox for the update, or buy the Beta now to get instant access. Enjoy the new version!

1 Like

Update: Beta 3.0!

The third Beta release of Kotlin Coroutine Confidence is ready, and thanks to your reader feedback it’s even Beta than before! Version 3.0 has smoother explanations of some key structured concurrency topics, and adds even more tips and advice to put those learnings in context. Special thanks to @dhmitchell for many helpful suggestions this time around.

Here’s what’s changed:

  • Cleared up some confusion about where the suspension-point icon appears.
  • Explained why the Timer app keeps running until we cancel() it.
  • Added some tips and advice about dispatchers and thread choices in specific code examples.
  • Improved the explanation of error propagation in the coroutine Job hierarchy.
  • Fixed some typos, and made several other small changes for brevity and clarity.

Check your inbox for the update, or buy the Beta now to get instant access. Thanks for reading, and keep on sharing your suggestions!

1 Like

Thanks

1 Like