JavaConcurrency
InPractice
BrianGöetz
TimPeierls
JoshuaBloch
JosephBowbeer
DavidHolmes
DougLea
AddisonWesleyProfessional
ISBN10:0321349601
ISBN13:9780321349606
ii
JavaConcurrencyInPractice
Index
Index
Preface
HowtoUsethisBook
CodeExamples
Acknowledgments
Chapter 1 - Introduction
1.1. A (Very) Brief History of Concurrency
1.2. Benefits of Threads
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.1.Visibility
3.2.PublicationandEscape
3.3.ThreadConfinement
3.4.Immutability
3.5.SafePublication
Chapter 4. Composing Objects
4.1.DesigningaThreadsafeClass
4.2.InstanceConfinement
4.3.DelegatingThreadSafety
4.4.AddingFunctionalitytoExistingThreadsafeClasses
4.5.DocumentingSynchronizationPolicies
Chapter 5. Building Blocks
5.1.SynchronizedCollections
5.2.ConcurrentCollections
5.3.BlockingQueuesandtheProducerconsumerPattern
5.4.BlockingandInterruptibleMethods
5.5.Synchronizers
5.6.BuildinganEfficient,ScalableResultCache
SummaryofPartI
ii
xiii
xiii
xiv
xv
1
2
3
3
3
3
4
5
5
6
6
8
10
11
12
13
16
19
20
23
23
26
28
31
33
37
37
39
41
47
49
51
51
54
56
59
60
64
69
Part II: Structuring Concurrent Applications
Chapter 6. Task Execution
6.1.ExecutingTasksinThreads
6.2.TheExecutorFramework
6.3.FindingExploitableParallelism
Summary
Chapter 7. Cancellation and Shutdown
7.1.TaskCancellation
7.2.StoppingaThreadbasedService
7.3.HandlingAbnormalThreadTermination
7.4.JVMShutdown
Summary
Chapter 8. Applying Thread Pools
8.1.ImplicitCouplingsBetweenTasksandExecutionPolicies
8.2.SizingThreadPools
8.3.ConfiguringThreadPoolExecutor
8.4.ExtendingThreadPoolExecutor
8.5.ParallelizingRecursiveAlgorithms
Summary
Chapter 9. GUI Applications
9.1.WhyareGUIsSinglethreaded?
9.2.ShortrunningGUITasks
9.3.LongrunningGUITasks
9.4.SharedDataModels
9.5.OtherFormsofSinglethreadedSubsystems
Summary
Part III: Liveness, Performance, and Testing
Chapter 10. Avoiding Liveness Hazards
10.1.Deadlock
10.2.AvoidingandDiagnosingDeadlocks
10.3.OtherLivenessHazards
Summary
Chapter 11. Performance and Scalability
11.1.ThinkingaboutPerformance
11.2.Amdahl'sLaw
11.3.CostsIntroducedbyThreads
11.4.ReducingLockContention
11.5.Example:ComparingMapPerformance
11.6.ReducingContextSwitchOverhead
Summary
Chapter 12. Testing Concurrent Programs
12.1.TestingforCorrectness
12.2.TestingforPerformance
12.3.AvoidingPerformanceTestingPitfalls
12.4.ComplementaryTestingApproaches
Summary
Part IV: Advanced Topics
Chapter 13 - Explicit Locks
13.1.LockandReentrantLock
13.2.PerformanceConsiderations
13.3.Fairness
iv
JavaConcurrencyInPractice
13.4.ChoosingBetweenSynchronizedandReentrantLock
13.5.ReadwriteLocks
Summary
Chapter 14 - Building Custom Synchronizers
14.1.ManagingStateDependence
14.2.UsingConditionQueues
14.3.ExplicitConditionObjects
14.4.AnatomyofaSynchronizer
14.5.AbstractQueuedSynchronizer
14.6.AQSinJava.util.concurrentSynchronizerClasses
Summary
Chapter 15. Atomic Variables and Non-blocking Synchronization
15.1.DisadvantagesofLocking
15.2.HardwareSupportforConcurrency
15.3.AtomicVariableClasses
15.4.NonblockingAlgorithms
Summary
Chapter 16. The Java Memory Model
16.1.WhatisaMemoryModel,andWhywouldIWantOne?
16.2.Publication
Summary
Appendix A. Annotations for Concurrency
A.1.ClassAnnotations
A.2.FieldandMethodAnnotations
Bibliography
176
176
178
179
179
183
188
189
190
192
194
195
195
196
198
201
206
207
207
211
215
216
216
216
217
1BListingandImageIndex
v
ListingandImageIndex
Preface
Listing1.BadWaytoSortaList.Don'tDothis.
Listing2.LessthanOptimalWaytoSortaList.
Chapter 1. Introduction
Listing1.1.NonthreadsafeSequenceGenerator.
Figure1.1.UnluckyExecutionofUnsafeSequence.Nextvalue.
Listing1.2.ThreadsafeSequenceGenerator.
Chapter 2. Thread Safety
Listing2.1.AStatelessServlet.
Listing2.2.ServletthatCountsRequestswithouttheNecessarySynchronization.Don'tDothis.
Listing2.3.RaceConditioninLazyInitialization.Don'tDothis.
Listing2.4.ServletthatCountsRequestsUsingAtomicLong.
Listing2.5.ServletthatAttemptstoCacheitsLastResultwithoutAdequateAtomicity.Don'tDothis.
Listing2.6.ServletthatCachesLastResult,ButwithUnacceptablyPoorConcurrency.Don'tDothis.
Listing2.7.CodethatwouldDeadlockifIntrinsicLockswereNotReentrant.
Figure2.1.PoorConcurrencyofSynchronizedFactorizer.
Listing2.8.ServletthatCachesitsLastRequestandResult.
Chapter 3. Sharing Objects
Listing3.1.SharingVariableswithoutSynchronization.Don'tDothis.
Listing3.2.NonthreadsafeMutableIntegerHolder.
Listing3.3.ThreadsafeMutableIntegerHolder.
Figure3.1.VisibilityGuaranteesforSynchronization.
Listing3.4.CountingSheep.
Listing3.5.PublishinganObject.
Listing3.6.AllowingInternalMutableStatetoEscape.Don'tDothis.
Listing3.7.ImplicitlyAllowingthethisReferencetoEscape.Don'tDothis.
Listing3.8.UsingaFactoryMethodtoPreventthethisReferencefromEscapingDuringConstruction.
Listing3.9.ThreadConfinementofLocalPrimitiveandReferenceVariables.
Listing3.10.UsingThreadLocaltoEnsurethreadConfinement.
Listing3.11.ImmutableClassBuiltOutofMutableUnderlyingObjects.
Listing3.12.ImmutableHolderforCachingaNumberanditsFactors.
Listing3.13.CachingtheLastResultUsingaVolatileReferencetoanImmutableHolderObject.
Listing3.14.PublishinganObjectwithoutAdequateSynchronization.Don'tDothis.
v
xiv
xv
11
5
5
6
11
13
14
15
16
17
18
18
21
21
23
23
24
24
25
26
27
27
28
28
30
30
32
33
33
33
vi
JavaConcurrencyInPractice
Listing3.15.ClassatRiskofFailureifNotProperlyPublished.
Chapter 4. Composing Objects
Listing4.1.SimpleThreadsafeCounterUsingtheJavaMonitorPattern.
Listing4.2.UsingConfinementtoEnsureThreadSafety.
Listing4.3.GuardingStatewithaPrivateLock.
Listing4.4.MonitorbasedVehicleTrackerImplementation.
Listing4.5.MutablePointClassSimilartoJava.awt.Point.
Listing4.6.ImmutablePointclassusedbyDelegatingVehicleTracker.
Listing4.7.DelegatingThreadSafetytoaConcurrentHashMap.
Listing4.8.ReturningaStaticCopyoftheLocationSetInsteadofa"Live"One.
Listing4.9.DelegatingThreadSafetytoMultipleUnderlyingStateVariables.
Listing4.10.NumberRangeClassthatdoesNotSufficientlyProtectItsInvariants.Don'tDothis.
Listing4.11.ThreadsafeMutablePointClass.
Listing4.12.VehicleTrackerthatSafelyPublishesUnderlyingState.
Listing4.13.ExtendingVectortohaveaPutifabsentMethod.
Listing4.14.NonthreadsafeAttempttoImplementPutifabsent.Don'tDothis.
Listing4.15.ImplementingPutifabsentwithClientsideLocking.
Listing4.16.ImplementingPutifabsentUsingComposition.
Chapter 5. Building Blocks
Listing5.1.CompoundActionsonaVectorthatmayProduceConfusingResults.
Figure5.1.InterleavingofGetlastandDeletelastthatthrows
ArrayIndexOutOfBoundsException.
Listing5.2.CompoundActionsonVectorUsingClientsideLocking.
Listing5.3.IterationthatmayThrowArrayIndexOutOfBoundsException.
Listing5.4.IterationwithClientsideLocking.
Listing5.5.IteratingaListwithanIterator.
Listing5.6.IterationHiddenwithinStringConcatenation.Don'tDothis.
Listing5.7.ConcurrentMapInterface.
Listing5.8.ProducerandConsumerTasksinaDesktopSearchApplication.
Listing5.9.StartingtheDesktopSearch.
Listing5.10.RestoringtheInterruptedStatussoasNottoSwallowtheInterrupt.
Listing5.11.UsingCountDownLatchforStartingandStoppingThreadsinTimingTests.
Listing5.12.UsingFutureTasktoPreloadDatathatisNeededLater.
Listing5.13.CoercinganUncheckedThrowabletoaRuntimeException.
Listing5.14.UsingSemaphoretoBoundaCollection.
34
37
37
39
40
42
42
42
43
43
44
45
46
46
47
48
48
49
51
51
51
52
52
52
53
54
56
58
58
60
61
62
62
64
1BListingandImageIndex
vii
Listing5.15.CoordinatingComputationinaCellularAutomatonwith
Listing5.16.InitialCacheAttemptUsingHashMapandSynchronization.
Figure5.2.PoorConcurrencyof
Figure5.3.TwoThreadsComputingtheSameValueWhenUsing
Listing5.17.ReplacingHashMapwithConcurrentHashMap.
Figure5.4.UnluckyTimingthatcouldCauseMemorizer3toCalculatetheSameValueTwice.
Listing5.18.MemorizingWrapperUsingFutureTask.
Listing5.19.FinalImplementationofMemorizer.
Listing5.20.FactorizingServletthatCachesResultsUsingMemorizer.
Chapter 6. Task Execution
Listing6.1.SequentialWebServer.
Listing6.2.WebServerthatStartsaNewThreadforEachRequest.
Listing6.3.ExecutorInterface.
Listing6.4.WebServerUsingaThreadPool.
Listing6.5.ExecutorthatStartsaNewThreadforEachTask.
Listing6.6.ExecutorthatExecutesTasksSynchronouslyintheCallingThread.
Listing6.7.LifecycleMethodsinExecutorService.
Listing6.8.WebServerwithShutdownSupport.
Listing6.9.ClassIllustratingConfusingTimerBehavior.
Listing6.10.RenderingPageElementsSequentially.
Listing6.11.CallableandFutureInterfaces.
Listing6.12.DefaultImplementationofnewTaskForinThreadPoolExecutor.
Listing6.13.WaitingforImageDownloadwithFuture.
Listing6.14.QueueingFutureClassUsedByExecutorCompletionService.
Listing6.15.UsingCompletionServicetoRenderPageElementsastheyBecomeAvailable.
Listing6.16.FetchinganAdvertisementwithaTimeBudget.
Listing6.17.RequestingTravelQuotesUnderaTimeBudget.
Chapter 7. Cancellation and Shutdown
Listing7.1.UsingaVolatileFieldtoHoldCancellationState.
Listing7.2.GeneratingaSecond'sWorthofPrimeNumbers.
Listing7.3.UnreliableCancellationthatcanLeaveProducersStuckinaBlockingOperation.Don'tDothis.
Listing7.4.InterruptionMethodsinThread.
Listing7.5.UsingInterruptionforCancellation.
Listing7.6.PropagatingInterruptedExceptiontoCallers.
Listing7.7.NoncancelableTaskthatRestoresInterruptionBeforeExit.
Listing7.8.SchedulinganInterruptonaBorrowedThread.Don'tDothis.
64
66
66
67
67
68
68
69
69
72
72
73
74
75
75
75
77
77
78
79
80
80
81
82
82
83
84
85
86
86
87
87
88
89
90
90
viii
JavaConcurrencyInPractice
Listing7.9.InterruptingaTaskinaDedicatedThread.
Listing7.10.CancellingaTaskUsingFuture.
Listing7.11.EncapsulatingNonstandardCancellationinaThreadbyOverridingInterrupt.
Listing7.12.EncapsulatingNonstandardCancellationinaTaskwithNewtaskfor.
Listing7.13.ProducerConsumerLoggingServicewithNoShutdownSupport.
Listing7.14.UnreliableWaytoAddShutdownSupporttotheLoggingService.
Listing7.15.AddingReliableCancellationtoLogWriter.
Listing7.16.LoggingServicethatUsesanExecutorService.
Listing7.17.ShutdownwithPoisonPill.
Listing7.18.ProducerThreadforIndexingService.
Listing7.19.ConsumerThreadforIndexingService.
Listing7.20.UsingaPrivateExecutorWhoseLifetimeisBoundedbyaMethodCall.
Listing7.21.ExecutorServicethatKeepsTrackofCancelledTasksAfterShutdown.
Listing7.22.UsingTRackingExecutorServicetoSaveUnfinishedTasksforLaterExecution.
Listing7.23.TypicalThreadpoolWorkerThreadStructure.
Listing7.24.UncaughtExceptionHandlerInterface.
Listing7.25.UncaughtExceptionHandlerthatLogstheException.
Listing7.26.RegisteringaShutdownHooktoStoptheLoggingService.
Chapter 8. Applying Thread Pools
Listing8.1.TaskthatDeadlocksinaSinglethreadedExecutor.Don'tDothis.
Listing8.2.GeneralConstructorforThreadPoolExecutor.
91
92
93
94
95
95
96
97
97
98
98
98
99
100
101
101
101
103
104
105
107
Listing8.3.CreatingaFixedsizedThreadPoolwithaBoundedQueueandtheCallerrunsSaturationPolicy. 109
Listing8.4.UsingaSemaphoretoThrottleTaskSubmission.
Listing8.5.ThreadFactoryInterface.
Listing8.6.CustomThreadFactory.
Listing8.7.CustomThreadBaseClass.
Listing8.8.ModifyinganExecutorCreatedwiththeStandardFactories.
Listing8.9.ThreadPoolExtendedwithLoggingandTiming.
Listing8.10.TransformingSequentialExecutionintoParallelExecution.
Listing8.11.TransformingSequentialTailrecursionintoParallelizedRecursion.
Listing8.12.WaitingforResultstobeCalculatedinParallel.
Listing8.13.AbstractionforPuzzlesLikethe"SlidingBlocksPuzzle".
Listing8.14.LinkNodeforthePuzzleSolverFramework.
Listing8.15.SequentialPuzzleSolver.
Listing8.16.ConcurrentVersionofPuzzleSolver.
Listing8.17.ResultbearingLatchUsedbyConcurrentPuzzleSolver.
109
109
110
111
111
112
112
113
113
113
114
115
115
116