www.it-ebooks.info 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 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 www.it-ebooks.info 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 www.it-ebooks.info Programming Concurrency on the JVM Mastering Synchronization, STM, and Actors Venkat Subramaniam The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina www.it-ebooks.info 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 transmi tte d, in any f or m, or by an y means, e lec tr oni c, 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 www.it-ebooks.info To Mom and Dad, for teaching the values of integrity, honesty, and diligence. www.it-ebooks.info Contents Preface . . . . . . . . . . . . . xi 1. The Power and Perils of Concurrency . . . . . . 1 Threads: The Flow of Execution 11.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 From Sequential to Concurrent 152.1 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 Dealing with State 353.1 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 www.it-ebooks.info Part II — Modern Java/JDK Concurrency 4. Scalability and Thread Safety . . . . . . . . 47 Managing Threads with ExecutorService 484.1 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 Shared Mutability != public 735.1 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 Synchronization Damns Concurrency 896.1 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 . 1 0 Configuring Akka Transactions 120 6 . 1 1 Blocking Transactions—Sensible Wait 122 6 . 1 2 Commit and Rollback Events 126 6 . 1 3 Collections and Transactions 129 6 . 1 4 Dealing with the Write Skew Anomaly 133 6 . 1 5 Limitations of STM 136 6 . 1 6 Recap 140 • viii www.it-ebooks.info 7. STM in Clojure, Groovy, Java, JRuby, and Scala . . . 141 Clojure STM 1427.1 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 Isolating Mutability Using Actors 1648.1 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 . 1 0 Using Transactors 202 8 . 1 1 Coordinating Typed Actors 210 8 . 1 2 Remote Actors 216 8 . 1 3 Limitations of the Actor-Based Model 218 8 . 1 4 Recap 219 9. Actors in Groovy, Java, JRuby, and Scala . . . . . 221 Actors in Groovy with GPars 2219.1 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 Exercise Your Options 2431 0 . 1 1 0 . 2 Concurrency: Programmer’s Guide 244 1 0 . 3 Concurrency: Architect’s Guide 245 1 0 . 4 Choose Wisely 246 • ix www.it-ebooks.info A1. Clojure Agents . . . . . . . . . . . 249 A2. Web Resources . . . . . . . . . . . 255 A3. Bibliography . . . . . . . . . . . . 259 Index . . . . . . . . . . . . . 261 x • Contents www.it-ebooks.info [...]... 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 we’re... for Concurrency, Modern Java/JDK Concurrency, Software Transactional Memory, Actor-Based Concurrency, and an epilogue In Chapter 1, The Power and Perils of Concurrency, on page 1, we will discuss what makes concurrency so useful and the reasons why it’s so hard to get it right This chapter will set the stage for the three concurrency models we’ll explore in this book Before we dive into these solutions,... depends on the number of cores available on the processor, and the number of concurrent threads for an application depends on the number of cores associated with its process 1.2 The Power of Concurrency We’re interested in concurrency for two reasons: to make an application responsive/improve the user experience and to make it faster Making Apps More Responsive When we start an application, the main... programming concurrency, this book is for you If you’re interested only in the solutions directly provided in Java and the JDK—Java threading and the concurrency library—I refer you to two very good books already on the market that focus on that: Brian Goetz’s Java Concurrency in Practice [Goe06] and Doug Lea’s Concurrent Programming in Java [Lea00] Those two books provide a wealth of information on. .. execution often takes on multiple responsibilities sequentially, depending on the actions we ask it to perform: receive input from a user, read from a file, perform some calculations, access a web service, update a database, display a response to the user, and so on If each of these operations takes only fractions of a second, then there may be no real need to introduce additional flows of execution;... conclude with some takeaway points that can help you succeed with concurrency Is it Concurrency or Parallelism? There’s no clear distinction between these two terms in the industry, and the number of answers we’ll hear is close to the number of people we ask for an explanation (and don’t ask them concurrently…or should I say in parallel?) Let’s not debate the distinction here We may run programs on. .. application makes use of multiple threads or concurrent flows of execution On a single processor, these concurrent tasks are often multiplexed or multitasked That is, the processor rapidly switches between the context of each flow of execution However, only one thread, and hence only one flow of execution, is performed at any given instance On a multicore processor, more than one flow of execution (thread)... the button, we won’t see any change the button does not change to “Stop,” and the label does not show the time count What’s worse, the application will not even respond to a quit request The main event dispatch thread is responsible for noticing UI-related events and delegating actions to be performed When the Start button is clicked, the main event dispatch thread went into the event handler actionPerformed();... Windows, run the program using the command java -server RaceCondition (asking it to be run in server mode on Windows), and on the Mac, run it using the command java -d32 RaceCondition (asking it to be run in client mode on the Mac) On Windows, we’d see something like this: OS: Windows 7 flag done set to true However, now on the Mac, we’ll see something like this: OS: Mac OS X Done! flag done set to true... primitives that makes the calling threads cross the memory barrier both when they enter and when they exit the synchronized block A thread is guaranteed to see the change made by another thread if both threads synchronize on the same instance and the change-making thread happens before the other thread; again, see What's This Memory Barrier?, on page 9 www.it-ebooks.info The Perils of Concurrency • 9 Joe . . xi 1. The Power and Perils of Concurrency . . . . . . 1 Threads: The Flow of Execution 11.1 1.2 The Power of Concurrency 2 1.3 The Perils of Concurrency. three separate concurrency solutions the modern Java JDK concurrency model, the Software Transac- tional Memory (STM), and the actor-based concurrency model. This