Java Concurrency In Practice

141 downloads 22100 Views 6MB Size Report
for All Things Concurrency for that, see Concurrent Programming in Java (Lea, 2000). Rather, it offers ..... The AWT (Abstract Window Toolkit) and Swing.
           

JavaConcurrency InPractice BrianGöetz TimPeierls JoshuaBloch JosephBowbeer DavidHolmes DougLea                     AddisonͲWesleyProfessional ISBNͲ10:0Ͳ321Ͳ34960Ͳ1 ISBNͲ13:978Ͳ0Ͳ321Ͳ34960Ͳ6



ii JavaConcurrencyInPractice 

Index Index Preface

ii xiii

HowtoUsethisBook CodeExamples Acknowledgments

xiii xiv xv

Chapter 1 - Introduction

1

1.1. A (Very) Brief History of Concurrency

2

1.2. Benefits of Threads

3

1.2.1.ExploitingMultipleProcessors 1.2.2.SimplicityofModeling 1.2.3.SimplifiedHandlingofAsynchronousEvents 1.2.4.MoreResponsiveUserInterfaces

1.3. Risks of Threads 1.3.1.SafetyHazards 1.3.2.LivenessHazards 1.3.3.PerformanceHazards

1.4. Threads are Everywhere

Part I: Fundamentals Chapter 2. Thread Safety 2.1.WhatisThreadSafety? 2.2.Atomicity 2.3.Locking 2.4.GuardingStatewithLocks 2.5.LivenessandPerformance

Chapter 3. Sharing Objects

3 3 3 4

5 5 6 6

8

10 11 12 13 16 19 20

23

3.1.Visibility 3.2.PublicationandEscape 3.3.ThreadConfinement 3.4.Immutability 3.5.SafePublication

23 26 28 31 33

Chapter 4. Composing Objects

37

4.1.DesigningaThreadͲsafeClass 4.2.InstanceConfinement 4.3.DelegatingThreadSafety 4.4.AddingFunctionalitytoExistingThreadͲsafeClasses 4.5.DocumentingSynchronizationPolicies

Chapter 5. Building Blocks 5.1.SynchronizedCollections 5.2.ConcurrentCollections 5.3.BlockingQueuesandtheProducerͲconsumerPattern 5.4.BlockingandInterruptibleMethods 5.5.Synchronizers 5.6.BuildinganEfficient,ScalableResultCache SummaryofPartI

37 39 41 47 49

51 51 54 56 59 60 64 69