...

/

Looking Deeper into Threads

Looking Deeper into Threads

Learn the basics of threads in Kotlin and what kind of problems they can solve.

We'll cover the following...

Before we dive into the nuances, let’s discuss what kinds of problems threads can solve.

In a laptop, we have a CPU with multiple cores—probably four of them, or even eight. This means that it can do four different computations in parallel, which is pretty amazing considering that 15 years ago, a single-core CPU was the default and even two cores were only for enthusiasts.

Press + to interact

But even back then, we were not limited to doing only a single task at a time, right? We could listen to music and browse the internet at the same time, even on a single-core CPU. How does our CPU manage to pull that off? Well, the same way our brain does. It juggles tasks. When we are reading a book while listening to our friend talking, part of the time, we are not reading, and part of the time, we are not listening—that is, until we get at least two cores in our brains.

The servers we run our code on have pretty much the same CPU. This means that they can serve four requests simultaneously. But what if we have 10,000 requests per second? We can’t serve them in parallel because we don’t have 10,000 CPU cores. But we can try and serve them concurrently.

...