Table of Contents
Copyright.................................................................................................................................................1
Dedication.........................................................................................................................................1
Advance Praise for Java Concurrency in Practice...................................................................................3
Listings.....................................................................................................................................................4
Preface......................................................................................................................................................8
How to Use this Book.....................................................................................................................11
Code Examples................................................................................................................................13
Acknowledgments...........................................................................................................................16
Chapter 1. Introduction..........................................................................................................................16
1.1. A (Very) Brief History of Concurrency...................................................................................21
1.2. Benefits of Threads..................................................................................................................24
1.3. Risks of Threads.......................................................................................................................27
1.4. Threads are Everywhere...........................................................................................................30
Part I: Fundamentals........................................................................................................................................34
Chapter 2. Thread Safety.......................................................................................................................34
2.1. What is Thread Safety?............................................................................................................37
2.2. Atomicity..................................................................................................................................41
2.3. Locking.....................................................................................................................................47
2.4. Guarding State with Locks.......................................................................................................50
2.5. Liveness and Performance........................................................................................................53
Chapter 3. Sharing Objects....................................................................................................................53
3.1. Visibility...................................................................................................................................57
3.2. Publication and Escape.............................................................................................................60
3.3. Thread Confinement.................................................................................................................64
3.4. Immutability.............................................................................................................................65
3.5. Safe Publication........................................................................................................................71
Chapter 4. Composing Objects..............................................................................................................76
4.1. Designing a Thread-safe Class...................................................................................................1
4.2. Instance Confinement.................................................................................................................1
4.3. Delegating Thread Safety...........................................................................................................1
4.4. Adding Functionality to Existing Thread-safe Classes..............................................................4
4.5. Documenting Synchronization Policies...................................................................................10
Chapter 5. Building Blocks....................................................................................................................17
5.1. Synchronized Collections.........................................................................................................18
5.2. Concurrent Collections.............................................................................................................19
5.3. Blocking Queues and the Producer-consumer Pattern.............................................................30
5.4. Blocking and Interruptible Methods.........................................................................................39
5.5. Synchronizers...........................................................................................................................41
5.6. Building an Efficient, Scalable Result Cache..........................................................................43
Summary of Part I...........................................................................................................................43
Part II: Structuring Concurrent Applications...............................................................................................44
Chapter 6. Task Execution.....................................................................................................................46
6.1. Executing Tasks in Threads.....................................................................................................47
6.2. The Executor Framework.........................................................................................................53
6.3. Finding Exploitable Parallelism...............................................................................................55
Summary.........................................................................................................................................60
Chapter 7. Cancellation and Shutdown..................................................................................................61
7.1. Task Cancellation.....................................................................................................................61
7.2. Stopping a Thread-based Service.............................................................................................63
7.3. Handling Abnormal Thread Termination.................................................................................65
Java Concurrency in Practice
i