Programming Concurrency on the JVM
Mastering Synchronization, STM, and Actors
by Venkat Subramaniam
Version: P1.0 (August 2011)
Copyright 2011 Pragmatic Programmers, LLC. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trust-don't allow othersto use your copy of the book. Thanks.
- Dave & Andy.
To Mom and Dad, for teaching the values of integrity, honesty, and diligence.
Table of Contents
- STM in Clojure, Groovy,
Java, JRuby, and Scala
Copyright 2011, The Pragmatic Bookshelf.
What Readers Are Saying About Programming Concurrency on the JVM
An excellent book! Venkat skillfully leads us through the many design and implementation decisions that todays JVM developer faces in multithreaded programming. His easy-to-read style and the many examples he providesusing a variety of current open source tools and JVM languagesmake this complex topic very approachable.
Albert Scherer |
Manager, eCommerce Technologies, Follett Higher Education Group, Inc. |
If the JVM is your platform of choice, then this book is an absolute must-read. Buy it, read it, and then buy a copy for all your team members. You will well be on your way to finding a good solution to concurrency issues.
Raju Gandhi |
Senior consultant, Integrallis Software, LLC |
Extremely thorough coverage of a critically important topic.
Chris Richardson |
Author of POJOS in Action and Founder, CloudFoundry.com |
There has been an explosion of interest and application for both new concurrency models and new languages on the JVM. Venkats book ties it all together and shows the working developer how to structure their application and get the most out of existing libraries, even if they were built in a different language. This book is the natural successor to Java Concurrency in Practice .
Alex Miller |
Architect/Senior Engineer, Revelytix, Inc. |
I found Programming Concurrency akin to sitting under a master craftsman imparting wisdom to his apprentice. The reader is guided on a journey that starts with the why of concurrency and the big-picture design issues that hell face. Hes then taught the modern concurrency tools provided directly within the Java SDK before embarking upon an adventure through the exciting realms of STM and actors. I sincerely believe that this book is destined to be one of the most important concurrency books yet written. Venkat has done it again!
Matt Stine |
Technical Architect, AutoZone, Inc. |
Concurrency is a hot topic these days, and Venkat takes you through a wide range of current techniques and technologies to program concurrency effectively on the JVM. More importantly, by comparing and contrasting concurrency approaches in five different JVM languages, you get a better picture of the capabilities of various tools you can use. This book will definitely expand your knowledge and toolbox for dealing with concurrency.
Scott Leberknight |
Chief Architect, Near Infinity Corporation |
Preface
Speed. Aside from caffeine, nothing quickens the pulse of a programmer as much as the blazingly fast execution of a piece of code.How can we fulfill the need for computational speed? Moores law takes us some of the way, but multicore is the real future. To take full advantage of multicore, we need to program with concurrency in mind.
In a concurrent program, two or more actions take place simultaneously. A concurrent program may download multiple files while performing computations and updating the database. We often write concurrent programs using threads in Java. Multithreading on the Java Virtual Machine (JVM) has been around from the beginning, but how we program concurrency is still evolving, as well learn in this book.
The hard part is reaping the benefits of concurrency without being burned. Starting threads is easy, but their execution sequence is nondeterministic. Were soon drawn into a battle to coordinate threads and ensure theyre handling data consistently.
To get from point A to point B quickly, we have several options, based on how critical the travel time is, the availability of transport, the budget, and so on. We can walk, take the bus, drive that pimped-up convertible, take a bullet train, or fly on a jet. In writing Java for speed, weve also got choices.
There are three prominent options for concurrency on the JVM:
What I call the synchronize and suffer model
The Software-Transactional Memory model
The actor-based concurrency model
I call the familiar Java Development Kit (JDK) synchronization model synchronize and suffer because the results are unpredictable if we forget to synchronize shared mutable state or synchronize it at the wrong level. If were lucky, we catch the problems during development; if we miss, it can come out in odd and unfortunate ways during production. We get no compilation errors, no warning, and simply no sign of trouble with that ill-fated code.
Programs that fail to synchronize access to shared mutable state are broken, but the Java compiler wont tell us that. Programming with mutability in pure Java is like working with the mother-in-law whos just waiting for you to fail. Im sure youve felt the pain.
There are three ways to avoid problems when writing concurrent programs:
Synchronize properly.
Dont share state.
Dont mutate state.
If we use the modern JDK concurrency API, well have to put in significant effort to synchronize properly. STM makes synchronization implicit and greatly reduces the chances of errors. The actor-based model, on the other hand, helps us avoid shared state. Avoiding mutable state is the secret weapon to winning concurrency battles.
In this book, well take an example-driven approach to learn the three models and how to exploit concurrency with them.
Whos This Book For?
Ive written this book for experienced Java programmers who are interested in learning how to manage and make use of concurrency on the JVM, using languages such as Java, Clojure, Groovy, JRuby, and Scala.
If youre new to Java, this book will not help you learn the basics of Java. There are several good books that teach the fundamentals of Java programming, and you should make use of them.
If you have fairly good programming experience on the JVM but find yourself needing material that will help further your practical understanding of programming concurrency, this book is for you.
If youre interested only in the solutions directly provided in Java and the JDKJava threading and the concurrency libraryI refer you to two very good books already on the market that focus on that: Brian Goetzs Java Concurrency in Practice [Goe06] and Doug Leas Concurrent Programming in Java [Lea00]. Those two books provide a wealth of information on the Java Memory Model and how to ensure thread safety and consistency.
My focus in this book is to help you use, but also move beyond, the solutions provided directly in the JDK to solve some practical concurrency problems. You will learn about some third-party Java libraries that help you work easily with isolated mutability. You will also learn to use libraries that reduce complexity and error by eliminating explicit locks.