iv.
Introduction
Coroutines with Kotlin represents one of the most interesting and fasinating challenges in the software engineering world. It’s an opportunity to implement complex concurrent tasks in an elegant and performant way. Reading this book will give you the opportunity to learn the basic concepts about multithreading and how concurrent programming can be simplified using Kotlin and Coroutines.
About Kotlin
Kotlin is a general purpose, open source, statically typed “pragmatic” programming language for the JVM that combines object-oriented and functional programming features. It originated at JetBrains, the company that drives IntelliJ IDEA, and has been open source since 2012.
At Google I/O 2017 Kotin was officially supported by Google as the main language for developing Android applications. It is a language focused on interoperability, safety, clarity, and tooling support.
It is also important to mention it’s multiplatform support with JavaScript (ECMAScript 5.1) and native code (using LLVM).
About Coroutines
Asynchronous programming is often tedious and error-prone. The extensive usage of callbacks makes the code hard to read, debug and test. Coroutines define a different paradigm which introduces the concept of suspending functions. Coroutines generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed. In this way, you can write asynchronous code as if it were synchronous. Coroutines are a recent solution in Kotlin and Android environment for writing concurrent and asynchronous code.
How to read this book
This book contains four sections.
The first section is an introduction to multithreading and concurrent programming with Kotlin. It explains how you can execute asynchronous tasks using the Kotlin language, what problems you may face and how coroutines can be a valid solution. You’ll be introduced to the fundamental concepts of suspending functions, coroutine context and dispatching. You’ll also learn how to manage exception and how to handle errors with coroutines.
The second section explains, in detail and with several examples, how to use very important API based on coroutine technology. You’ll learn how to create sequences and iterators. Using channels, you’ll learn how different coroutines can communicate and exchange data in thread safe way. Finally you’ll learn all the details about coroutine flow which are a fundamental part of the last version.
In the first two sections, you learned everything about coroutines. If you need some specific knowledge about how to use them in the Android environment, this is the section for you. You’ll create a complete Android application and you’ll see how to use coroutines in order to create a very responsive application.
Kotlin can be used on JVM but also in other enviroments; this is called multiplatform. In this forth section you’ll learn how to use coroutine with other languages like Java.
The best way to learn about Kotlin Coroutines is to roll up your sleeves and start coding. Enjoy the book!