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)

Channels - Share Memory by Communicating

Many errors related to concurrency happen when memory is shared between different threads, for example, having an object that will be modified by different threads. Sharing memory this way is dangerous because unless there's bulletproof synchronization, there will be scenarios in which the shared object will enter a state that it should never enter—and writing bulletproof synchronization is more difficult than it may seem.

Deadlocks, race conditions, and atomicity violations are related to shared states. Sometimes they happen because a shared state is invalid, and other times they will cause the state to become inconsistent.

In order to overcome these issues, modern programming languages like Kotlin, Go, and Dart provide channels. Channels are tools that will help you write concurrent code, which, instead of sharing a state...