Kotlin coroutines have revolutionized JVM development, especially on Android and the backend, as they let us easily implement efficient and reliable multithreading. Their cutting-edge design and features are ideally suited to modern use cases. In this book, we will explore how Kotlin coroutines work and how we can use them to improve our code.
This is a practical book. It shows everything by examples and focuses on real-life use cases. As this technology is already widely used on Android and backend applications, we will see how we can use it there and what the best practices are.
The importance of concurrency is growing, but traditional techniques are not enough. Current trends suggest that coroutines are the direction in which our industry is clearly heading, and Kotlin Coroutines represent a significant step forward. Let me introduce them to you, along with examples of how well they handle common use cases. I hope you will have a lot of fun reading this book.
The book is divided into the following parts:
• Part 1: Understanding Kotlin Coroutines - dedicated to explaining what Kotlin Coroutines are and how they really work.
• Part 2: Kotlin Coroutines library - explaining the most important concepts from the kotlinx.coroutines library and how to use them well.
• Part 3: Channel and Flow - focused on Channel and Flow from the kotlinx.coroutines library.
• Part 4: Kotlin Coroutines in practice - reviewing common use cases and the most important best practices for using Kotlin Coroutines.
Who is this book for?
As a developer experienced in both backend and Android, I primarily focus on these two perspectives in this book. These are currently the two major industry applications of Kotlin, and it’s evident that coroutines were largely designed to suit these use cases well1. So, you might say that this book is mainly designed for Android and backend developers, but it should be just as useful for other developers using Kotlin. This book assumes that readers know Kotlin well. If you do not, I recommend starting with my other book, Kotlin Essentials.
Author(s): Marcin Moskała
Publisher: leanpub.com
Year: 2021
Language: English
Pages: 469
Part 1: Understanding Kotlin Coroutines
Why Kotlin Coroutines?
Sequence builder
How does suspension work?
Coroutines under the hood
Coroutines: built-in support vs library
Part 2: Kotlin Coroutines library
Coroutine builders
Coroutine context
Jobs and awaiting children
Cancellation
Exception handling
Coroutine scope functions
Dispatchers
Constructing a coroutine scope
The problem with shared state
Testing Kotlin Coroutines
Part 3: Channel and Flow
Channel
Select
Hot and cold data sources
Flow introduction
Understanding Flow
Flow building
Flow lifecycle functions
Flow processing
SharedFlow and StateFlow
Testing flow
Part 4: Kotlin Coroutines in practice
Common use cases
Recipes
Using coroutines from other languages
Launching coroutines vs. suspending functions
Best practices
The End
Notes