Actors in Scala
Philipp Haller is a post-doctoral researcher at Stanford University, USA,and EPFL, Switzerland.Frank Sommers is president of Autospaces, Inc.
Artima Press is an imprint of Artima, Inc.
P.O. Box 305, Walnut Creek, California 94597
Copyright 2010-2011 Philipp Haller and Frank Sommers. All rights reserved.
PrePrint Edition first published 2010
First edition published 2011
Build date of this impression December 23, 2011
Produced in the United States of America
No part of this publication may be reproduced, modified, distributed,stored in a retrieval system, republished, displayed, or performed, forcommercial or noncommercial purposes or for compensation of any kind without prior written permission from Artima, Inc.
All information and materials in this book are provided "as is" and without warranty of any kind.
The term "Artima" and the Artima logo are trademarks or registered trademarks of Artima, Inc. All other company and/or product namesmay be trademarks or registered trademarks of their owners.
to my parents, Gudrun and Rudolf - P.H.
to Jungwon - F.S.
Table of Contents
Foreword
I have been fascinated by concurrency and distributed computing for as long as I can remember. I stillremember the joy of solving the classic dining philosophers problem using my own semaphore library builton top of the pthreads C library back in university many years ago. A few years later a new language calledJava came along. Java made a lot of things easier for us developers; it had, for example, automatic memorymanagement and a rich standard library. It also came with a single unified abstraction over threads and hadconcurrency utilities built into the language (for example, wait , notify , and synchronized ), which was later expandedwith the rich java.util.concurrent library. I embraced Java as my primary language of choice. The problemwas that writing concurrent programs still needed too much low-level plumbing and was still way too hard. Themain reason for this was that Java blindly adopted the C/C++ concurrency paradigm; shared-state concurrency,e.g., concurrent access to shared mutable state, which in most cases is not what you need and therefore the wrong default.
Over the years I have done my share of programming with threads and locks, spending late nights and weekends atthe office tracking down concurrency bugs. One day I just had enough. I started reading up on alternativeconcurrency paradigms, in particular message-passing concurrency as implemented by the language and runtime Erlang,and dataflow concurrency from an esoteric academic language called Oz. I was amazed by both of these languages andtheir approach to concurrency; in particular, Erlang and its actor-based programming model. Actors raised the abstractionlevel from threads and locks and made it so much easier to write, understand, and maintain my concurrent code. Insteadof spending time getting the plumbing right, I could focus on the workflowhow the messages flowed in the systemwhichmade my code so much easier to understand and maintain. So I fell in love with Erlang and its actors. Actors also turnedout to be great not only for concurrency but also for fault tolerance and distributed computing. I had finally found atool that I could use to scale up, utilizing symmetric multiprocessor (SMP)/multicore machines, scale out on the cluster, and write highlyavailable systems. Life was good.
About five years ago I discovered and fell in love with Scala. In Scala, I found a fascinating language that blendedobject-oriented and functional programming with rare elegance, ran on the JVM, was statically typed with the sameperformance as Java, and...had actors. It couldn't be better. All the goodness of Erlang on the JVM. I startedusing Scala and Scala's actors as my main language and tool set for writing scalable, fault-tolerant, and highlyconcurrent systems. This eventually led me to create Akka, an actor runtime that builds upon the Scala foundationby adding even richer tools for concurrency and distributed computing.
I have a lot to thank Philipp Haller, one of the two authors of this book and the creator of Scala actors. Withouthis excellent work I would probably not have started using Scala and would not have created Akka. I would have missedout on a lot of fun. I am really glad that Philipp and Frank have taken the time to write this book. Now we finallyhave an introductory book that teaches how to use actors in Scala and that covers both Scala's actors library as wellas Akka. It is a great first step into the wonderful world of actors. See reading this book as an investment: it will help you to write correct, performant, scalable, and available systems in less time, giving more time to more important things in life.
Jonas Bonr
CTO Typesafe
Uppsala, Sweden
November 1, 2011
Acknowledgments
There are many people who have shaped this book in differentways. First and foremost, we'd like to thank Tom Van Cutsemfor contributing large parts of ("Distributed and ParallelComputing"); at least half of the material on MapReduce wascontributed by him. In the same chapter, the solutions for best-effortand reliable broadcast are based on code contributed by AleksandarProkopec.
We are grateful to Kunle Olukotun and Martin Odersky for supportingPhilipp's work on this book in 2011, while being a post-doctoralresearch fellow at Stanford University, USA, and at colePolytechnique Fdrale de Lausanne (EPFL), Switzerland.
We'd also like to thank Stphane Micheloud, Samira Tasharofi, MircoDotta, and Vojin Jovanovic, for reviewing earlier versions andpreprints of this book. Their feedback has helped us improve the textin countless places.
Special thanks go to Bill Venners who initially came up with the ideaof a book on Scala actors. Without his expertise in publishingand his continuous support over many months, this book would not havebeen possible.
Introduction
This book is a tutorial for programming with actors in the Scalaprogramming language. Actors provide a high-level programming modelfor concurrent and distributed systems based on lightweight processesand message passing. One of the authors of this book, Philipp Haller,is the creator of Scala's actors library, and is directly involved inthe development of actors in Scala's standard library. Our goal isthat by reading this book, you can learn everything you need to buildefficient, scalable, and robust concurrent software for the JVM,productively, using actors. All examples in this book compile withScala version 2.9.1 and Akka version 1.2.
Who should read this book
The main target audience of this book is programmers who want to learnhow to program using actors in Scala. If you are interested inbuilding highly concurrent, scalable systems in Scala, then this bookintroduces you to Scala's main offering in this space, actors. Thisbook provides an introduction both to actors in Scala's standardlibrary, as well as to Akka's actors, which enable even more powerfulfacilities for fault-tolerant event-driven middleware solutions.
In addition, students and programmers who would like to expand theirknowledge of concurrent and distributed programming by learning abouta not-yet-standard concurrency paradigm should find this bookinteresting. As a Java programmer, for example, you might be used toprogramming with threads, synchronized methods, volatile fields, andso on. Learning about actors, and the underlying approach to modelingconcurrent systems, can help you think about concurrency differently,thereby broadening your horizons.