Book Image

Learning Concurrency in Kotlin

By : Miguel Angel Castiblanco Torres
Book Image

Learning Concurrency in Kotlin

By: Miguel Angel Castiblanco Torres

Overview of this book

Kotlin is a modern and statically typed programming language with support for concurrency. Complete with detailed explanations of essential concepts, practical examples and self-assessment questions, Learning Concurrency in Kotlin addresses the unique challenges in design and implementation of concurrent code. This practical guide will help you to build distributed and scalable applications using Kotlin. Beginning with an introduction to Kotlin's coroutines, you’ll learn how to write concurrent code and understand the fundamental concepts needed to write multithreaded software in Kotlin. You'll explore how to communicate between and synchronize your threads and coroutines to write collaborative asynchronous applications. You'll also learn how to handle errors and exceptions, as well as how to work with a multicore processor to run several programs in parallel. In addition to this, you’ll delve into how coroutines work with each other. Finally, you’ll be able to build an Android application such as an RSS reader by putting your knowledge into practice. By the end of this book, you’ll have learned techniques and skills to write optimized code and multithread applications.
Table of Contents (11 chapters)

Preface

The era when the speed of a single core in a processor would duplicate every few years is long over. Transistors have reached unbelievably small sizes, yet heating, power consumption, and other technical and practical challenges have made it difficult to keep increasing their speed in a user-friendly, affordable way. Even more so if you consider not only desktop computers but also mobile devices such as smartphones and tablets, which are universal nowadays. So, in recent years, the focus has been put on equipping these computing devices with more than one core or more than one processor – running with a balanced configuration of speed, heating, and power consumption.

The challenge becomes using those cores efficiently and effectively. An increase in the speed of a core will directly improve the execution time of the software running on it – for CPU-bound operations – but that is not the case when another core or processor is added. For an application to be able to take advantage of multiple cores, it needs to be written with that objective in mind. It needs to be concurrent.

However, concurrency not only benefits applications that will run in multiple cores. A concurrent application will usually work faster than a single-threaded application for IO-bound operations, even if running in a single core, because it allows the application to use the processor while other tasks are waiting for IO devices. For example, while waiting for a network operation.

So it's in your hands, as the developer, the person reading this book, to write the software that will work efficiently both in a single-core device and in a multi-core one. It's in my hands, the writer, to provide you with enough information, expressed in an understandable way, to achieve this goal. With this in mind, I have written this book, expecting nothing but basic Kotlin knowledge from you. I have tried to distill everything, from the very concepts of concurrency to the very specifics of how coroutines are compiled, for you. I have done it so that we both can do our part in bringing our users – be it humans or other systems a fast yet reliable experience.

This book covers various topics in great detail, using tools such as diagrams, examples, and more often than not, real-life use cases. For that reason, most of the chapters follow the creation and modification of an RSS reader for Android, completely written in Kotlin and using only the tools and primitives provided by the language and the coroutines library. I encourage you to follow along the creation of the application and to type the code instead of copying it – especially if you have only the printed version of the book so that you get to interiorize how to do it. I encourage you even more to go off-script and try some things for yourself; if you start wondering whether something can be accomplished in a certain way, it means that your brain is beginning to get a grasp of how to apply the concepts. And there's nothing better than trying something to know if it works.

One thing that you should be aware of is that while coroutines are being developed for all the platforms that Kotlin supports JVM, JavaScript, and Kotlin/Native at the time of writing this book, the most complete implementation is that of JVM. For that reason, I have written this book centered around what is available and how things work for the JVM. However, know that many of the concepts and primitives work in a similar fashion for JavaScript, and I believe they will work the same way in Kotlin/Native once implemented.

There are still many topics that I wasn't able to write about, but I do believe that the contents of this book are enough to get you to a point from where you can confidently move forward on you own, and I also believe that you will be able to write many of your concurrent applications based on what will be covered in these pages.

This puts the future squarely in the hands of those who know computers not for what they are, but the potential of what they can be
- Gordon
Computers aren't the thing, they are the thing that gets us to the thing
- Joe