Learning Concurrency in Kotlin
Build highly efficient and robust applications
Miguel Angel Castiblanco Torres
BIRMINGHAM - MUMBAI
Learning Concurrency in Kotlin
Copyright 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Aaron Lazar
Acquisition Editor: Sandip Mishra
Content Development Editor: Anugraha Arunagiri
Technical Editor: Jijo Maliyekal
Copy Editor: Safis Editing
Project Coordinator: Ulhas Kambali
Proofreader: Safis Editing
Indexer: Tejal Daruwale Soni
Graphics: Tania Dutta
Production Coordinator: Arvindkumar Gupta
First published: July 2018
Production reference: 1270718
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78862-716-0
www.packtpub.com
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com , you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors
About the author
Miguel Angel Castiblanco Torres
is a software engineer living in the United States. He works as a full-stack technical leader and software designer at Globant, where he has led many successful projects for a Forbes' Top Ten World's Most Valuable Brand and Top Five Regarded Company.
Passionate about what's next, Miguel was an early adopter of Kotlin, writing about Kotlin's concurrency primitives from the first beta release of coroutines. He always keeps an eye on the new and upcoming features of Kotlin.
About the reviewer
Peter Sommerhoff helps you to always keep learning, master new skills, and, ultimately, achieve your goals. Does that sound like something you'd like to do? If yes, ensure that you enroll for one of his courses and learn about programming languages, software design, productivity, or anything else you're interested in! He will ensure that you have a great learning experience. You can always ask him personally if you have anything at all that he can help you with. So, check out his courses, and he'll see you there!
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
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.
Next page