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 Java platform has evolved, from the synchronization model of JDK to software transactional memory (STM) and actor-based concurrency. This book is the first to show you all these concurrency styles so you can compare and choose what works best for your applications. You'll learn the benefits of each of these models, when and how to use them, and what their limitations are. Through hands-on exercises, you'll learn how to avoid shared mutable state and how to write good, elegant, explicit synchronization-free programs so you can create easy and safe concurrent applications. The techniques you learn in this book will take you from dreading concurrency to mastering and enjoying it. Best of all, you can work with Java or a JVM language of your choice - Clojure, JRuby, Groovy, or Scala - to reap the growing power of multicore hardware. If you are a Java programmer, you'd need JDK 1.5 or later and the Akka 1.0 library. In addition, if you program in Scala, Clojure, Groovy or JRuby you'd need the latest version of your preferred language. Groovy programmers will also need GPars.
Author(s): Venkat Subramaniam
Edition: 1
Publisher: Pragmatic Bookshelf
Year: 2011
Language: English
Pages: 282
Cover......Page 1
Table of Contents......Page 7
Preface......Page 11
Who's This Book For?......Page 12
What's in This Book?......Page 13
Concurrency for Polyglot Programmers......Page 14
Examples and Performance Measurements......Page 15
Acknowledgments......Page 16
Threads: The Flow of Execution......Page 18
The Power of Concurrency......Page 19
The Perils of Concurrency......Page 22
Recap......Page 27
Part 1—Strategies for Concurrency......Page 29
From Sequential to Concurrent......Page 30
Concurrency in IO-Intensive Apps......Page 33
Concurrency in Computationally Intensive Apps......Page 40
Speedup for the Computationally Intensive App......Page 46
Strategies for Effective Concurrency......Page 48
Recap......Page 49
Dealing with State......Page 50
Exploring Design Options......Page 51
Isolated Mutable Design......Page 52
Purely Immutable Design......Page 53
Persistent/Immutable Data Structures......Page 54
Selecting a Design Approach......Page 57
Recap......Page 58
Part 2—Modern Java/JDK Concurrency......Page 60
4. Scalability and Thread Safety......Page 61
Managing Threads with ExecutorService......Page 62
Coordinating Threads......Page 63
Exchanging Data......Page 72
Java 7 Fork-Join API......Page 75
Scalable Collections......Page 77
Lock vs. Synchronized......Page 80
Recap......Page 85
Shared Mutability != public......Page 86
Spotting Concurrency Issues......Page 87
Preserve Invariant......Page 88
Mind Your Resources......Page 89
Ensure Visibility......Page 92
Enhance Concurrency......Page 93
Ensure Atomicity......Page 95
Recap......Page 98
Part 3—Software Transactional Memory......Page 99
Synchronization Damns Concurrency......Page 100
The Deficiency of the Object Model......Page 101
Separation of Identity and State......Page 102
Software Transactional Memory......Page 103
Transactions in STM......Page 107
Concurrency Using STM......Page 108
Concurrency Using Akka/Multiverse STM......Page 113
Creating Transactions......Page 115
Creating Nested Transactions......Page 122
Configuring Akka Transactions......Page 131
Blocking Transactions—Sensible Wait......Page 133
Commit and Rollback Events......Page 137
Collections and Transactions......Page 140
Dealing with the Write Skew Anomaly......Page 144
Limitations of STM......Page 147
Recap......Page 151
7. STM in Clojure, Groovy, Java, JRuby, and Scala......Page 152
Groovy Integration......Page 153
Java Integration......Page 157
JRuby Integration......Page 160
Choices in Scala......Page 167
Recap......Page 169
Part 4—Actor-Based Concurrency......Page 171
8. Favoring Isolated Mutability......Page 172
Isolating Mutability Using Actors......Page 173
Actor Qualities......Page 174
Creating Actors......Page 175
Sending and Receiving Messages......Page 182
Working with Multiple Actors......Page 187
Coordinating Actors......Page 191
Using Typed Actors......Page 199
Typed Actors and Murmurs......Page 204
Mixing Actors and STM......Page 210
Using Transactors......Page 211
Coordinating Typed Actors......Page 219
Remote Actors......Page 225
Limitations of the Actor-Based Model......Page 227
Recap......Page 228
Actors in Groovy with GPars......Page 229
JRuby Akka Integration......Page 243
Recap......Page 247
Part 5—Epilogue......Page 248
Exercise Your Options......Page 249
Concurrency: Programmer's Guide......Page 250
Concurrency: Architect's Guide......Page 251
Choose Wisely......Page 252
A1. Clojure Agents......Page 255
A2. Web Resources......Page 260
A3. Bibliography......Page 263
Index......Page 265