Chapter 6: Threads and Coroutines
In the previous chapter, we had a glance at how our application can efficiently serve thousands of requests per second—to discuss why immutability is important, we introduced a race condition problem using two threads.
In this chapter, we'll dive deeper into how to launch new threads in Kotlin and the reasons why coroutines can scale much better than threads. We will discuss how the Kotlin compiler treats coroutines and the relationship between coroutine scopes and dispatchers. We'll discuss the concept of structured concurrency, and how it helps us prevent resource leaks in our programs.
We'll cover the following topics in this chapter:
- Looking deeper into threads
- Introducing coroutines and suspend functions
- Starting coroutines
- Jobs
- Coroutines under the hood
- Dispatchers
- Structured concurrency
After reading this chapter, you'll be familiar with Kotlin's concurrency primitives...