Programming Concurrency on the JVM - The Pragmatic Bookshelf

2 downloads 219 Views 408KB Size Report
This PDF file contains pages extracted from Programming Concurrency on the JVM, published by the ... 2.5 Speedup for the
Extracted from:

Programming Concurrency on the JVM Mastering Synchronization, STM, and Actors

This PDF file contains pages extracted from Programming Concurrency on the JVM, published by the Pragmatic Bookshelf. For more information or to purchase a paperback or PDF copy, please visit http://www.pragprog.com . Note: This extract contains some colored text (particularly in code listing). This is available only in online versions of the books. The printed versions are black and white. Pagination might vary between the online and printer versions; the content is otherwise identical. Copyright © 2010 The 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.

The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina

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 trademarks 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 . 1.

The 1.1 1.2 1.3 1.4

.

.

.

.

.

.

.

Power and Perils of Concurrency Threads: The Flow of Execution The Power of Concurrency The Perils of Concurrency Recap

. .

. .

. .

.

.

.

?

.

.

.

? ? ? ? ?

2.

Division of Labor . . . . . . . . . 2.1 From Sequential to Concurrent 2.2 Concurrency in IO-Intensive Apps 2.3 Speedup for the IO-Intensive App 2.4 Concurrency in Computationally Intensive Apps 2.5 Speedup for the Computationally Intensive App 2.6 Strategies for Effective Concurrency 2.7 Recap

.

.

? ? ? ? ? ? ? ?

3.

Design Approaches . . . . . . . 3.1 Dealing with State 3.2 Exploring Design Options 3.3 Shared Mutable Design 3.4 Isolated Mutable Design 3.5 Purely Immutable Design 3.6 Persistent/Immutable Data Structures 3.7 Selecting a Design Approach 3.8 Recap

Part I — Strategies for Concurrency

.

.

.

.

? ? ? ? ? ? ? ? ?



vi

Part II — Modern Java/JDK Concurrency 4.

Scalability and Thread Safety . . . . 4.1 Managing Threads with ExecutorService 4.2 Coordinating Threads 4.3 Exchanging Data 4.4 Java 7 Fork-Join API 4.5 Scalable Collections 4.6 Lock vs. Synchronized 4.7 Recap

.

.

.

.

? ? ? ? ? ? ? ?

5.

Taming Shared Mutability . . 5.1 Shared Mutability != public 5.2 Spotting Concurrency Issues 5.3 Preserve Invariant 5.4 Mind Your Resources 5.5 Ensure Visibility 5.6 Enhance Concurrency 5.7 Ensure Atomicity 5.8 Recap

.

.

.

.

? ? ? ? ? ? ? ? ?

.

.

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

.

.

.

Part III — Software Transactional Memory 6.

Introduction to Software Transactional Memory . 6.1 Synchronization Damns Concurrency 6.2 The Deficiency of the Object Model 6.3 Separation of Identity and State 6.4 Software Transactional Memory 6.5 Transactions in STM 6.6 Concurrency Using STM 6.7 Concurrency Using Akka/Multiverse STM 6.8 Creating Transactions 6.9 Creating Nested Transactions 6.10 Configuring Akka Transactions 6.11 Blocking Transactions—Sensible Wait 6.12 Commit and Rollback Events 6.13 Collections and Transactions 6.14 Dealing with the Write Skew Anomaly 6.15 Limitations of STM 6.16 Recap

.

• 7.

STM 7.1 7.2 7.3 7.4 7.5 7.6

in Clojure, Groovy, Java, JRuby, and Scala . Clojure STM Groovy Integration Java Integration JRuby Integration Choices in Scala Recap

vii

.

.

.

? ? ? ? ? ? ?

.

.

.

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

Part IV — Actor-Based Concurrency 8.

Favoring Isolated Mutability . . . . 8.1 Isolating Mutability Using Actors 8.2 Actor Qualities 8.3 Creating Actors 8.4 Sending and Receiving Messages 8.5 Working with Multiple Actors 8.6 Coordinating Actors 8.7 Using Typed Actors 8.8 Typed Actors and Murmurs 8.9 Mixing Actors and STM 8.10 Using Transactors 8.11 Coordinating Typed Actors 8.12 Remote Actors 8.13 Limitations of the Actor-Based Model 8.14 Recap

9.

Actors in Groovy, Java, JRuby, and Scala 9.1 Actors in Groovy with GPars 9.2 Java Integration 9.3 JRuby Akka Integration 9.4 Choices in Scala 9.5 Recap

.

.

.

.

.

.

.

? ? ? ? ? ?

.

.

.

.

.

? ? ? ? ?

Part V — Epilogue 10.

Zen 10.1 10.2 10.3 10.4

of Programming Concurrency . . Exercise Your Options Concurrency: Programmer’s Guide Concurrency: Architect’s Guide Choose Wisely

viii



Contents

A1. Clojure Agents .

.

.

.

.

.

.

.

.

.

.

.

?

A2. Web Resources .

.

.

.

.

.

.

.

.

.

.

.

?

A3. Bibliography Index

.

.

.

.

.

.

.

.

.

.

.

.

.

.

?

.

.

.

.

.

.

.

.

.

.

.

.

?