Taming Java Threads.pdf
(
1478 KB
)
Pobierz
Microsoft Word - Taming Java Threads.doc
Table of Contents
Taming Java Threads
Preface
Chapter 1
-
The Architecture of Threads
Chapter 2
-
The Perils of Multithreaded Programming
Chapter 3
The Mutex and Lock Management
-
Chapter 4
Condition Variables and Counting Semaphores
Chapter 5
-
-
Timers, Alarms, and Swing Thread Safety
Chapter 6
-
Observers and Multicasters
Chapter 7
-
Singletons, Critical Sections, and Reader/Writer Locks
Chapter 8
-
Threads in an Object-Oriented World
Chapter 9
-
Object-Oriented Threading Architectures
Chapter 10
-
If I Were King: Fixing Java's Threading Problems
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
2
Taming Java Threads .......................................................................................................... 7
Preface................................................................................................................................. 8
Prerequisites
................................................................................................................. 8
Getting and Using the Code
...................................................................................... 8
Chapter 1: The Architecture of Threads
............................................ 10
The Problems with Threads
..................................................................................... 10
All Nontrivial Java Programs Are Multithreaded ..................................................... 10
Java's Thread Support Is Not Platform Independent ................................................ 12
Threads and Processes
.............................................................................................. 13
Thread Safety and Synchronization
....................................................................... 13
Synchronization Is Expensive................................................................................... 14
Avoiding Synchronization ........................................................................................ 18
Concurrency, or How Can You Be Two Places at Once (When You're Really
Nowhere at All)
......................................................................................................... 22
Get Your Priorities Straight
..................................................................................... 23
Cooperate!
................................................................................................................... 24
The Cooperative Multithreading Model ................................................................... 24
The Preemptive Multithreading Model..................................................................... 25
Mapping Kernel Threads to User Processes ............................................................. 25
Wrapping Up
.............................................................................................................. 28
Chapter 2: The Perils of Multithreaded Programming
........... 29
Monitors and Exclusion Semaphores (Mutex)
.................................................... 29
Race Conditions and Spin Locks
............................................................................ 30
The Spin_lock Class ................................................................................................. 35
Threads Are Not Objects
......................................................................................... 39
Deadlock
..................................................................................................................... 41
Get out the Magnifying Glass
................................................................................. 42
Nested-Monitor Lockout
.......................................................................................... 44
Synchronization Wrappers........................................................................................ 45
Time Out!
.................................................................................................................... 46
A Digression on Style ............................................................................................... 49
Why Is
suspend()
Deprecated?
............................................................................... 50
Deadlock on a Blocking I/O Operation
................................................................. 51
Stopping Threads
....................................................................................................... 54
Starvation and Synchronizing
run()
....................................................................... 55
The
volatile
Keyword
............................................................................................... 57
Exceptions and Threads
........................................................................................... 58
3
Conclusion
.................................................................................................................. 60
Chapter 3: The Mutex and Lock Management
............................. 61
When
synchronized
Isn't Good Enough
................................................................ 61
Handling Granularity with
synchronized
.............................................................. 62
Roll Your Own Semaphores: The
Semaphore
Interface
.................................... 63
Managing Semaphores and Deadlock-Resistant Locking
................................. 66
A Digression: Booch Utilities and Strategy.............................................................. 70
Implementing a Manageable
Mutex
Class
............................................................ 71
Conclusion
.................................................................................................................. 77
Chapter 4: Condition Variables and Counting Semaphores
78
Condition Variables
.................................................................................................. 78
Waiting for the Electrician (or Somebody Like Him): Condition Variables vs. wait()
................................................................................................................................... 78
Send in the Cavalry: Using a Condition Variable..................................................... 81
Implementing a Condition Variable.......................................................................... 89
Condition Sets: Waiting for Multiple Conditions
............................................... 98
Counting Semaphores for Managing Resource Pools
...................................... 106
Wrapping Up
............................................................................................................ 115
Chapter 5: Timers, Alarms, and Swing Thread Safety
........ 116
Why a Timer?
........................................................................................................... 116
Swingin' Threads: Swing Isn't Thread Safe
........................................................ 117
The invokeLater() and invokeAndWait() Methods ................................................ 118
Using the Swing Timer
.......................................................................................... 119
So, How Does It Work? .......................................................................................... 121
Why Use a Swing Timer (or Not)........................................................................... 121
Roll Your Own Timer: Implementing the Alarm Class
................................... 122
The Static Structure of an Alarm ............................................................................ 124
Dissecting a Notification......................................................................................... 125
Restarting an Alarm (Stopping a Thread) ............................................................... 141
Suspending the Clock ............................................................................................. 142
Notifier Problems.................................................................................................... 142
Unit Tests ................................................................................................................ 143
Summing Up
............................................................................................................ 144
Chapter 6: Observers and Multicasters
............................................ 145
Implementing Observer in a Multithreaded World
........................................... 145
Observer-side Problems: Inner-class Synchronization
.................................... 146
Notifier-side Problems: Notifications in a Multithreaded World
................... 149
Mysteries of the
AWTEventMulticaster
.............................................................. 157
Immutable Objects and Blank Finals...................................................................... 158
Using the Multicaster.............................................................................................. 159
Building a Multicaster
............................................................................................ 160
4
Chapter 7: Singletons, Critical Sections, and Reader/Writer
Locks
.......................................................................................................................... 168
Overview
................................................................................................................... 168
Critical Sections, Singletons, and the "Class Object"
...................................... 168
Static Members ....................................................................................................... 168
Singletons
.................................................................................................................. 172
Critical Sections, Doubled-checked Locking, and Cache-related Problems in
Multiple-CPU Machines ......................................................................................... 174
The Std Class: An Example of Singleton ............................................................... 176
Closing Singletons .................................................................................................. 184
Reader/Writer Locks
............................................................................................... 185
It's a Wrap
................................................................................................................. 198
Chapter 8: Threads in an Object-Oriented World
.................... 199
Overview
................................................................................................................... 199
Modeling Threads in Object-Oriented Systems
................................................ 199
Synchronous vs. Asynchronous Messages ............................................................. 199
Implementing Asynchronous Messages Using Thread-per-Method ...................... 200
An Exceptional Problem ......................................................................................... 205
Thread Pools and Blocking Queues
..................................................................... 206
Blocking Queues..................................................................................................... 208
Pooling Threads ...................................................................................................... 220
Passing Arguments to the Operation....................................................................... 221
Using Introspection for Runnable Objects that Pass Arguments............................ 222
Implementing the
Thread_pool
............................................................................... 228
Putting the Pool to Work......................................................................................... 239
Sockets and Thread Pools
...................................................................................... 240
Conclusion
................................................................................................................ 253
Chapter 9: Object-Oriented Threading Architectures
........... 254
Reactors and Active Objects
................................................................................. 254
Synchronous Dispatching and Round-Robin Scheduling: Reactors and
Proactors
.................................................................................................................... 255
Asynchronous Dispatching: Active Objects
...................................................... 266
A General Solution ................................................................................................. 267
Detangling Console Output .................................................................................... 272
That's It
...................................................................................................................... 278
Chapter 10: If I Were King: Fixing Java's Threading
Problems
.................................................................................................................. 280
Overview
................................................................................................................... 280
The Task
.................................................................................................................... 280
Improvements to
synchronized
............................................................................. 283
Improvements to
wait()
and
notify()
.................................................................... 284
5
Plik z chomika:
r41lblast
Inne pliki z tego folderu:
A Tester's Guide to .NET Programming.pdf
(20132 KB)
A Programmer's Introduction to PHP 4.0.pdf
(4311 KB)
Accelerated CSharp 2010.pdf
(7008 KB)
Accelerated CSharp 2008.pdf
(6166 KB)
Adobe Coldfusion Anthology.pdf
(31579 KB)
Inne foldery tego chomika:
Pliki dostępne do 01.06.2025
C#
c# - C sharp
Dokumenty
Galeria
Zgłoś jeśli
naruszono regulamin