Table Of ContentWhat Readers Are Saying About
Programming Concurrency on the JVM
An excellent book! Venkat skillfully leads us through the many design and
implementation decisions that today’s JVM developer faces in multithreaded
programming. His easy-to-read style and the many examples he provides—using
a variety of current open source tools and JVM languages—make 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. Venkat’s 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 im-
parting 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 he’ll face.
He’s 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 var-
ious tools you can use. This book will definitely expand your knowledge and
toolbox for dealing with concurrency.
➤ Scott Leberknight
Chief Architect, Near Infinity Corporation
Programming Concurrency on
the JVM
Mastering Synchronization, STM, and Actors
Venkat Subramaniam
The Pragmatic Bookshelf
Dallas, Texas • Raleigh, North Carolina
Many of the designations used by manufacturers and sellers to distinguish their products
are claimed as trademarks. Where those designations appear in this book, and The Pragmatic
Programmers, LLC was aware of a trademark claim, the designations have been printed in
initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer,
Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade-
marks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes
no responsibility for errors or omissions, or for damages that may result from the use of
information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create
better software and have more fun. For more information, as well as the latest Pragmatic
titles, please visit us at http://pragprog.com.
The team that produced this book includes:
Brian P. Hogan (editor)
Potomac Indexing, LLC (indexer)
Kim Wimpsett (copyeditor)
David Kelly (typesetter)
Janet Furlow (producer)
Juliet Benda (rights)
Ellie Callahan (support)
Copyright © 2011 Pragmatic Programmers, LLC.
All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or
transmitted, in any form, or by any means, electronic, mechanical, photocopying,
recording, or otherwise, without the prior consent of the publisher.
Printed in the United States of America.
ISBN-13: 978-1-934356-76-0
Printed on acid-free paper.
Book version: P1.0—August 2011
To Mom and Dad, for teaching the values of
integrity, honesty, and diligence.
Contents
Preface . . . . . . . . . . . . . xi
1. The Power and Perils of Concurrency . . . . . . 1
1.1 Threads: The Flow of Execution 1
1.2 The Power of Concurrency 2
1.3 The Perils of Concurrency 5
1.4 Recap 10
Part I — Strategies for Concurrency
2. Division of Labor . . . . . . . . . . . 15
2.1 From Sequential to Concurrent 15
2.2 Concurrency in IO-Intensive Apps 18
2.3 Speedup for the IO-Intensive App 25
2.4 Concurrency in Computationally Intensive Apps 25
2.5 Speedup for the Computationally Intensive App 31
2.6 Strategies for Effective Concurrency 33
2.7 Recap 34
3. Design Approaches . . . . . . . . . . 35
3.1 Dealing with State 35
3.2 Exploring Design Options 36
3.3 Shared Mutable Design 37
3.4 Isolated Mutable Design 37
3.5 Purely Immutable Design 38
3.6 Persistent/Immutable Data Structures 39
3.7 Selecting a Design Approach 42
3.8 Recap 43
• viii
Part II — Modern Java/JDK Concurrency
4. Scalability and Thread Safety . . . . . . . . 47
4.1 Managing Threads with ExecutorService 48
4.2 Coordinating Threads 49
4.3 Exchanging Data 58
4.4 Java 7 Fork-Join API 61
4.5 Scalable Collections 63
4.6 Lock vs. Synchronized 66
4.7 Recap 71
5. Taming Shared Mutability . . . . . . . . . 73
5.1 Shared Mutability != public 73
5.2 Spotting Concurrency Issues 74
5.3 Preserve Invariant 75
5.4 Mind Your Resources 76
5.5 Ensure Visibility 79
5.6 Enhance Concurrency 80
5.7 Ensure Atomicity 82
5.8 Recap 85
Part III — Software Transactional Memory
6. Introduction to Software Transactional Memory . . . 89
6.1 Synchronization Damns Concurrency 89
6.2 The Deficiency of the Object Model 90
6.3 Separation of Identity and State 91
6.4 Software Transactional Memory 92
6.5 Transactions in STM 96
6.6 Concurrency Using STM 97
6.7 Concurrency Using Akka/Multiverse STM 102
6.8 Creating Transactions 104
6.9 Creating Nested Transactions 111
6.10 Configuring Akka Transactions 120
6.11 Blocking Transactions—Sensible Wait 122
6.12 Commit and Rollback Events 126
6.13 Collections and Transactions 129
6.14 Dealing with the Write Skew Anomaly 133
6.15 Limitations of STM 136
6.16 Recap 140
• ix
7. STM in Clojure, Groovy, Java, JRuby, and Scala . . . 141
7.1 Clojure STM 142
7.2 Groovy Integration 142
7.3 Java Integration 146
7.4 JRuby Integration 149
7.5 Choices in Scala 156
7.6 Recap 158
Part IV — Actor-Based Concurrency
8. Favoring Isolated Mutability . . . . . . . . 163
8.1 Isolating Mutability Using Actors 164
8.2 Actor Qualities 165
8.3 Creating Actors 166
8.4 Sending and Receiving Messages 173
8.5 Working with Multiple Actors 178
8.6 Coordinating Actors 182
8.7 Using Typed Actors 190
8.8 Typed Actors and Murmurs 195
8.9 Mixing Actors and STM 201
8.10 Using Transactors 202
8.11 Coordinating Typed Actors 210
8.12 Remote Actors 216
8.13 Limitations of the Actor-Based Model 218
8.14 Recap 219
9. Actors in Groovy, Java, JRuby, and Scala . . . . . 221
9.1 Actors in Groovy with GPars 221
9.2 Java Integration 235
9.3 JRuby Akka Integration 235
9.4 Choices in Scala 239
9.5 Recap 239
Part V — Epilogue
10. Zen of Programming Concurrency . . . . . . 243
10.1 Exercise Your Options 243
10.2 Concurrency: Programmer’s Guide 244
10.3 Concurrency: Architect’s Guide 245
10.4 Choose Wisely 246
x • Contents
A1. Clojure Agents . . . . . . . . . . . 249
A2. Web Resources . . . . . . . . . . . 255
A3. Bibliography . . . . . . . . . . . . 259
Index . . . . . . . . . . . . . 261
Description:More than ever, learning to program concurrency is critical to creating faster, responsive applications. Speedy and affordable multicore hardware is driving the demand for high-performing applications, and you can leverage the Java platform to bring these applications to life. Concurrency on the Jav