JavaConcurrencyInPractice Index Index retRace HowtoUsethisbook Codeexamples Acknowledgments Chapter1-Introduction 1.1.A(Very)BriefHistoryofConcurrency 1.2.Benefitsofthreads 233 1.2.1.ExploitingMultipleprocessors 1.2.2.Simplicityofmodeling 1.2.3.SimplifiedHandlingofasynchronousEvents nn 1.2.4MoreresponsiveUserinterfaces 4 1,3.Risksofthreads 1.3.1.Safetyhazards cnnnn onnnnmndmnnn 1.3.2.Livenesshazards 6 1.3.3Performancehazards 1.4.ThreadsareEverywhere 8 Partl:fundamentals 10 Chapter2.ThreadSafety 2.1.WhatisThreadsafety? 2.2.Atomicity 13 2.4.GuardingstatewithLocks 19 2.5.Livenessandperformance Chapter3.sharingObjects. 23 3.1.Visibility 23 3.2.Publicationandescape 口m 3.3.ThreadConfinement 3.4.Immutability 31 3.5.Safepublication 33 Chapter4.composingobject 37 4.1.DesigningaThread-safeClass 37 4.2.InstanceConfinement 39 4.3.Delegating.Threadsafety 4.4.AddingfunctionalitytoexistingThread-safeclasses 4.5DocumentingSynchronizationpolicies 49 Chapter5Buildingblocks 51 5.1.Synchronizedcollectio 5.2ConcurrentCollections 54 5.3.Blockingqueuesandtheproducer-consumerpattern 5.4.BlockingandInterruptibleMethods 5.5.Synchronizers 60 5.6.BuildinganEfficientScalableresultCache 64 Summaryofpart 69 m口口m Index PartII:StructuringConcurrentApplications Chapter6TaskExecution 72 6.1.ExecutingTasksinThreads 6.2.Theexecutorframework 6.3.Findingexploitableparallelism 78 Summary 83 Chapter7.CancellationandShutdown 85 71.TaskCancellation 85 7.2.StoppingaThread-basedservice 73.HandlingAbnormalThreadtermination 100 7.4.VMShutdown m口 102 Summary 103 Chapter8.ApplyingThreadpools 104 8.1/mplicitCouplingsBetweenTasksandExecutionpolicies 104 8.2.SizingThreadpools 8.3.ConfiguringThreadpoolexecutor 8.4.ExtendingthreadPoolExecutor 1 8.5.ParallelizingRecursiveAlgorithms 112 Summary 11 Chapter9.GUlapplications 117 9.1.WhyaregUlsSingle-threaded? 117 9.2.Short-runninggU!Tasks 119 9.3.Long-runninggulTasks 121 9.4.Shareddatamodels 9.5.OtherFormsofsingle-threadedsubsystems Summary 126 PartlllR:Liveness,Performance,andTesting 27 Chapter10.Avoidinglivenesshazard 128 10.1.Deadlock 128 10.2.AvoidingandDiagnosingDeadlocks 133 10.3.Otherlivenesshazards 135 Summary 136 Chapter11.Performanceandscalability 137 11.1.ThinkingaboutPertormance..--- 137 11.2.Amdahl'slaw 139 11.3CostsIntroducedbythreads 142 11.4.ReducingLockContention 144 11.5.Example:ComparingMapPerformance 150 11.6.ReducingContextSwitchOverhead 15 Summary 152 crentprogan 12.1.Testingforcorrectness 153 12.2.TestingforPerformance 12.3.AvoidingPerformanceTesting.pitfalls 12.4.Complementarytestingapproaches 167 Summary 169 Part/V:Advancedtopics 170 Chapter13-ExplicitLocks 13.1.Lockandreentrantlock 13.2.PerformanceConsiderations 13.3fairness 175 InnuI ivJavaConcurrencyInPractice 13.4.ChoosingBetweenSynchronizedandReentrantLock 176 13.5.Read-writelocks 176 Summary 178 Chapter14-Buildingcustomsynchronizers 179 14.1.ManagingStateDependence 179 14.2.UsingConditionQueues 183 14.3.ExplicitConditionObjects 188 14.4.Anatomyofgsynchronizer 189 14.5.AbstractqueuedSynchronizer 14.6.AQsinJava.util.concurrentSynchronizerclasses 192 Summary 194 Chapter15.AtomicVariablesandNon-blockingSynchronization. 15.1.DisadvantagesofLocking 195 15.2.HardwareSypportforconcurrency 196 153.Atomicvariableclasses 198 15.4.Non-blockingAlgorithms Summary Chapter16 Mode 207 16.1.WhatisaMemorymodelandwhywould/WantOne? 16.2Publication Summary 215 AppendixaannotationsforConcurrency 216 A,1.Classannotations 216 A.2.FieldandmethodAnnotations 216 Bibliograph 217 1BListingandImageIndexv ListingandImageIndex Preface Listing1.BadWaytoSortaList.Don'tDothis. XIV Listing2.LessthanOptimalWaytosortaList. Chapter1Introduction Listing1.1.Non-thread-safeSequenceGenerator. Figure1.1.UnluckyExecutionofUnsafesequenceNextvalue Listing1.2.Thread-safeSequenceGenerator. apter 2Threadsafety 11 Listing2.1.AStatelessServlet. Listing2.2.ServletthatCountsRequestswithouttheNecessarySynchronization.DontDothis. Listing2.3.RaceConditioninLazyInitialization.Don'tDothis. 5 Listing2.4.ServletthatCountsRequestsUsingAtomiCLong Listing2.5.ServletthatAttemptstoCacheitsLastResultwithoutAdequateatomicity.Don'tDothis.17 Listing2.6.ServletthatcachesLastResut,ButwithUnacceptabyPoorconcurency.DontDothis,18 Listing2.7.CodethatwouldDeadlockifIntrinsicLockswereNotreentrant. 18 Figure2.1.PoorConcurrencyofSynchronizedFactorizer Listing2.8.ServletthatCachesitsLastRequestandResult. 21 Chapter3.SharingObjects Listing3.1.SharingVariableswithoutSynchronization.Don'tDothis Listing3.2.Non-thread-safeMutableIntegerHolder. 24 Listing3.3.Thread-safeMutableIntegerHolder. 24 Figure3.1.visibilityGuaranteesforSynchronization. Listing3.4.Countingsheep 26 Listing3.5.Publishinganobject. 27 Listing3.6.AllowingInternalMutableStatetoEscape.Don'tDothis 27 Listing3.7.ImplicitlyAllowingthethisReferencetoEscape.Don'tDothis. 28 Listing.8.UsingaFactoryMethodtoPreventthethisReferencefromEscapingDuringConstruction.28 Listing3.9.ThreadConfinementofLocalPrimitiveandReferenceVariables. 30 Listing3.10.UsingThreadLocaltoEnsurethreadConfinement. 30 Listing3.11.ImmutableClassBuiltOutofMutableUnderlyingobjects. Listing3.12.ImmutableHolderforCachingaNumberanditsFactors. Listing3.13.CachingtheLastResultUsingaVolatileReferencetoanImmutableHolderObject. 33 Listing3.14.PublishinganObjectwithoutAdequateSynchronization.Don'tDothis. 33 viJavaConcurrencyInPractice Listing3.15.ClassatRiskofFailureifNotProperlypublished. 34 Chapter4.ComposingObiects Listing4.1.SimpleThread-safeCounterUsingtheJavaMonitorPattern. Listing4.2.UsingConfinementtoEnsureThreadSafety. 39 Listing4.3.GuardingStatewithaPrivateLock 40 Listing4.4.Monitor-basedVehicleTrackerImplementation. 42 Listing4.5.MutablePointClassSimilartoJava.awt.Point. Listing4.6.Immutablepointclassusedbydelegatingvehicletracker 42 Listing4.7.DelegatingThreadSafetytoaConcurrentHashMap. Listing4.8.ReturningaStaticCopyoftheLocationSetInsteadofa"LiveOne 43 Listing4.9.DelegatingThreadSafetytoMultipleUnderlyingStatevariables Listing4.10.NumberRangeClassthatdoesnotSufficientlyProtectItsInvariants.Don'tDothis. 45 Listing4.11.Thread-safeMutablePointClass 46 Listing4.12.VehicleTrackerthatSafelyPublishesUnderlyingState. 46 Listing4.13.ExtendingvectortohaveaPut-if-absentMethod. 47 Listing4.14.Non-thread-safeAttempttoImplementPut-if-absent.Don'tDothis. 48 Listing4.15.ImplementingPut-if-absentwithClient-sideLocking. 48 Listing4.16.ImplementingPut-if-absentUsingComposition. 49 Chapter5Buildingblocks 51 Listing5.1.CompoundActionsonavectorthatmayproduceConfusingResults Figure5.1.InterleavingofGetlastandDeletelastthatthrows ArrayIndexoutofBoundsException. Listing5.2.CompoundActionsonVectorUsingClient-sideLocking. 52 Listing5.3.iterationthatmayThrowArrayIndexoutofBoundsException 52 Listing5.4.IterationwithClient-sideLocking. ting5.5iteratingaListwithanIterator 53 Listing5.6.IterationHiddenwithinStringConcatenation.Don'tDothis 54 Listing5.7.ConcurrentMapInterface. Listing5.8.ProducerandConsumerTasksinaDesktopSearchApplication. Listing5.9.StartingtheDesktopSearch. 58 Listing5.10.RestoringtheInterruptedStatussoasNottoSwallowtheInterrupt. Listing5.11.UsingCountDownLatchforStartingandStoppingthreadsinTimingTests. 61 Listing5.12.UsingFutureTasktoPreloadDatathatisNeededLater. Listing5.13.CoercinganUncheckedThrowabletoaRuntimeException. 62 Listing5.14.UsingSemaphoretoBoundaCollection. BListingandImageIndexvii Listing5.15.CoordinatingComputationinaCellularAutomatonwith 64 Listing5.16InitialCacheAttemptUsingHashMapandSynchronization. Figure5.2.PoorConcurrencyof 66 Figure5.3.TwoThreadscomputingtheSamevalueWhenUsing 67 Listing5.17.ReplacingHashMapwithConcurrentHashMap 67 Figure5.4.UnluckyTimingthatcouldCauseMemorizer3toCalculatetheSameValueTwice. Listing5.18.MemorizingWrapperUsingFutureTask 68 Listing5.19.FinalImplementationofmemorizer. 69 Listing5.20.FactorizingServletthatCachesResultsUsingMemorizer 69 Chapter6.TaskExecution Listing6.1.SequentialWebserver. Listing6.2.WebServerthatStartsaNewThreadforEachRequest. 73 Listing6.3.EXecutorInterface. 74 Listing6.4.WebServerUsingaThreadPool. 75 Listing6.5.EXecutorthatStartsaNewThreadforEachTask. Listing6.6.ExecutorthatExecutesTasksSynchronouslyintheCallingThread. Listing6.7.LifecycleMethodsinEXecutorservice 77 Listing6.8.WebServerwithShutdownSupport. 77 Listing6.9.ClassIllustratingConfusingTimerBehavior. 78 Listing6.10.RenderingPageElementsSequentially. 79 Listing.11.CallableandFutureInterfaces. Listing6.12.DefaultImplementationofnewTaskForinThreadPoolEXecutor. 80 Listing6.13.WaitingforImageDownloadwithFuture. Listing6.14.QueueingFutureClassUsedByExecutorcompletionservice. 82 Listing6.15.UsingcompletionservicetoRenderpageelementsastheybecomeavailable. 82 Listing6.16.FetchinganAdvertisementwithaTimeBudget. 83 Listing6.17.RequestingtravelQuotesunderatimebudget Chapter7.Cancellationandshutdown. 85 Listing7.1.UsingavolatilefieldtoHoldCancellationState. Listing7.2.GeneratingaSecondsWorthofPrimeNumbers. Listing7.3.UnreliableCancellationthatcanLeaveProducersStuckinaBlockingOperation.Don'tDothis.87 Listing7.4.InterruptionMethodsinThread. 87 Listing7.5.UsingInterruptionforCancellation. 88 Listing7.6.PropagatingInterruptedExceptiontoCaller Listing7.7.Non-cancelableTaskthatRestoresInterruptionBeforeExit. Listing7.8.SchedulinganInterruptonaborrowedThread.Don'tDothis. viiiJavaConcurrencyInPractice Listing7.9.InterruptingaTaskinaDedicatedThread. 91 Listing7.10.CancellingaTaskUsingFuture. 92 Listing7.11.EncapsulatingNonstandardCancellationinaThreadbyOverridingInterrupt. 93 Listing7.12.EncapsulatingNonstandardCancellationinaTaskwithNewtaskfor. 94 Listing713Producer-ConsumerLoggingServicewithNoShutdownSupport. Listing714UnreliableWaytoAddshutdownSupporttotheLoggingService. 95 Listing7.15.AddingreliableCancellationtoLogwriter. Listing7.16.LoggingServicethatUsesanEXecutorservice 97 Listing717.ShutdownwithPoisonPill. 97 Listing7.18.ProducerThreadforIndexingservice 98 Listing7.19.ConsumerThreadforIndexingService 98 Listing7.20.UsingaPrivateExecutorWhoselifetimeisboundedbyamethodcall. 98 Listing7.21.EXecutorservicethatKeepsTrackofCancelledTasksAfterShutdown. 99 Listing7.22.UsingTRackingExecutorservicetoSaveUnfinishedTasksforLaterExecution. 100 Listing723.TypicalThread-poolWorkerThreadStructure. 101 Listing7.24.UncaughtExceptionHandlerInterface 101 Listing7.25.UncaughtExceptionHandlerthatlogstheException. 101 Listing7.26.RegisteringaShutdownHooktoStoptheLoggingService. 103 Chapter8.ApplyingThreadpools 104 Listing8.1.taskthatDeadlocksinaSingle-threadedExecutor.DontDothis. 105 Listing.2.GeneralConstructorforThreadPoolexecutor. 107 Listing8.3.CreatingaFixed-sizedThreadPoolwithaBoundedqueueandtheCaller-runsSaturationPolicy.109 Listing.4.UsingaSemaphoretoThrottleTaskSubmission. Listing8.5.ThreadFactoryInterface 109 Listing8.6.CustomThreadFactory. 110 Listing.7.CustomThreadBaseClass. 111 Listing8.8.ModifyinganExecutorcreatedwiththeStandardFactories. Listing.9.ThreadPoolExtendedwithLoggingandTiming. Listing.10.TransformingSequentialExecutionintoParallelExecution. Listing8.11.TransformingSequentialTail-recursionintoParallelizedRecursion. 113 Listing8.12.WaitingforResultstobeCalculatedinParallel. 113 Listing8.13.AbstractionforPuzzlesliketheSlidingblocksPuzzle". 113 Listing8.14.LinkNodeforthePuzzleSolverFramework. 114 Listing8.15.SequentialPuzzleSolver. 115 Listing8.16.ConcurrentVersionofPuzzlesolver. 115 Listing8.17.Result-bearingLatchUsedbyConcurrentpuzzresolver 116 1BListingandImageIndex Listing8.18.SolverthatRecognizeswhenNoSolutionExists. 116 Chapter9.GUIApplications 117 Figure9.1.ControlFlowofaSimpleButtonclick. 119 Listing9.1.Implementingswingutilitiesusinganexecutor 120 Listing9.2.ExecutorBuiltAtopSwingUtilities. 120 Listing9.3.SimpleEventListener. 120 Figure9.2.ControlFlowwithSeparateModelandViewObjects. 121 Listing.4.BindingaLong-runningTasktoaVisualComponent. 121 Listing9.5.Long-runningTaskwithUserFeedback. 122 Listing9.6.CancellingaLong-runningTask. 122 Listing9.7.BackgroundTaskClassSupportingCancellation,CompletionNotification,andProgressNotification. 124 Listing9.8.InitiatingaLong-running,CancellableTaskwithBackgroundtask 124 napter10.Avoidingliy des 128 Figure10.1.UnluckyTiminginLeftRightDeadlock. 128 Listing10.1.SimpleLock-orderingDeadlockDontDothis. 129 Listing10.2.DynamicLock-orderingDeadlockDon'tDothi 129 Listing10.3.InducingaLockOrderingtoAvoidDeadlock. 130 Listing10.4.DriverLoopthatInducesDeadlockUnderTypicalConditions. 131 Listing10.5.Lock-orderingDeadlockBetweenCooperatingObjects.Don'tDothis. 132 Listing10.6.UsingOpenCallstoAvoidingDeadlockBetweenCooperatingObjects. 133 Listing10.7.PortionofThreadDumpAfterDeadlock 135 ertorman SC 137 Figure11.1.MaximumUtilizationUnderAmdahI'sLawforVariousSerializationPercentages. 140 Listing11.1.SerializedAccesstoaTaskQueue. 141 Figure11.2.ComparingQueueImplementations. Listing11.2.SynchronizationthathasNoEffect.Don'tDothis. 142 Listing11.3.CandidateforLockElision. 143 Listing11.4.HoldingaLockLongerthanNecessary. 145 Listing11.5.ReducingLockDuration. 145 Listing11.6.CandidateforLockSplitting. 146 Listing11.7.serverstatusRefactoredtousesplitlocks. 146 Listing11.8.Hash-basedMapUsingLockStriping. 148 Figure11.3.ComparingScalabilityofMapImplementations. 150 Chapter12.TestingConcurrentPrograms 153 Listing12.1.BoundedBufferUsingSemaphore. 154