IndexIndex iiPreface xiiiHow to Use this Book xiiiCode Examples xivAcknowledgments xvChapter 1 - Introduction11.1. A (Very) Brief History of Concurrency21.2. Benefits of Threads31.2.1. Exploiting Multiple Processors31.2.2. Simplicity of Modeling31.2.3. Simplified Handling of Asynchronous Events31.2.4. More Responsive User Interfaces41.3. Risks of Threads51.3.1. Safety Hazards51.3.2. Liveness Hazards61.3.3. Performance Hazards61.4. Threads are Everywhere8Part I: Fundamentals 10Chapter 2. Thread Safety 112.1. What is Thread Safety? 122.2. Atomicity 132.3. Locking 162.4. Guarding State with Locks 192.5. Liveness and Performance 20Chapter 3. Sharing Objects 233.1. Visibility 233.2. Publication and Escape 263.3. Thread Confinement 283.4. Immutability 313.5. Safe Publication 33Chapter 4. Composing Objects 374.1. Designinga Thread‐safe Class 374.2. Instance Confinement 394.3. Delegating Thread Safety 414.4. AddingFunctionality to Existing Thread‐safe Classes 474.5. Documenting Synchronization Policies 49Chapter 5. Building Blocks 515.1. Synchronized Collections 515.2. Concurrent Collections 545.3. Blocking Queues and the Producer‐consumer Pattern 565.4. Blocking and Interruptible Methods 595.5. Synchronizers 605.6. Building an Efficient, Scalable Result Cache 64Summary of PartI 69<Index iiiPart II: Structuring Concurrent Applications 71Chapter 6. Task Execution 726.1. Executing Tasks in Threads 726.2. The Executor Framework 746.3. Finding Exploitable Parallelism 78Summary 83Chapter 7. Cancellation and Shutdown 857.1. Task Cancellation 857.2. Stoppinga Thread‐based Service 937.3. Handling Abnormal Thread Termination 1007.4. JVM Shutdown 102Summary 103Chapter 8. Applying Thread Pools 1048.1. Implicit Couplings Between Tasks and Execution Policies 1048.2. Sizing Thread Pools 1058.3. Configuring ThreadPoolExecutor 1068.4. Extending ThreadPoolExecutor 1118.5. Parallelizing Recursive Algorithms 112Summary 116Chapter 9. GUI Applications 1179.1. Why are GUIs Single‐threaded? 1179.2. Short‐running GUI Tasks 1199.3. Long‐running GUI Tasks 1219.4. Shared Data Models 1239.5. Other Forms of Single‐threaded Subsystems 125Summary 126Part III: Liveness, Performance, and Testing 127Chapter 10. Avoiding Liveness Hazards 12810.1. Deadlock 12810.2. Avoiding and Diagnosing Deadlocks 13310.3. Other Liveness Hazards 135Summary 136Chapter 11. Performance and Scalability 13711.1. Thinking about Performance 13711.2. Amdahl's Law 13911.3. CostsIntroduced by Threads 14211.4. Reducing Lock Contention 14411.5. Example: Comparing Map Performance 15011.6. Reducing Context Switch Overhead 151Summary 152Chapter 12. Testing Concurrent Programs 15312.1. Testing for Correctness 15312.2. Testing for Performance 16012.3. Avoiding Performance Testing Pitfalls 16512.4. Complementary Testing Approaches 167Summary 169Part IV: Advanced Topics 170Chapter 13 - Explicit Locks 17113.1. Lock and ReentrantLock 17113.2. Performance Considerations 17413.3. Fairness 175iv Java Concurrency In Practice13.4. Choosing Between Synchronized and ReentrantLock 17613.5. Read‐write Locks 176Summary 178Chapter 14 - Building Custom Synchronizers 17914.1. Managing State Dependence 17914.2. Using Condition Queues 18314.3. Explicit Condition Objects 18814.4. Anatomy of a Synchronizer 18914.5. AbstractQueuedSynchronizer 19014.6. AQS in Java.util.concurrent Synchronizer Classes 192Summary 194Chapter 15. Atomic Variables and Non-blocking Synchronization 19515.1. Disadvantages of Locking 19515.2. Hardware Support for Concurrency 19615.3. Atomic Variable Classes 19815.4. Non‐blocking Algorithms 201Summary 206Chapter 16. The Java Memory Model 20716.1. What is a Memory Model, and Why would I Want One? 20716.2. Publication 211Summary 215Appendix A. Annotations for Concurrency 216A.1. Class Annotations 216A.2. Field and Method Annotations 216Bibliography 217
评论