| |
| |
Introduction | |
| |
| |
| |
Threads | |
| |
| |
| |
Introduction to Threads | |
| |
| |
What Is a Thread? | |
| |
| |
Why Use Multiple Threads? | |
| |
| |
When Multiple Threads Might Not Be Good | |
| |
| |
Java's Built-in Thread Support | |
| |
| |
Basy to Start, Tough to Master | |
| |
| |
| |
A Simple Two-Thread Example | |
| |
| |
Extending the java.lang.Thread Class | |
| |
| |
Overriding the run() Method | |
| |
| |
Spawning a New Thread | |
| |
| |
Putting It All Together | |
| |
| |
Summary | |
| |
| |
| |
Crealing and Starting a Thread | |
| |
| |
Using Thread.currentThread() | |
| |
| |
Naming a Thread: getName() and setName() | |
| |
| |
Thread Constructors | |
| |
| |
Enlivening a Thread: start() and isAlive() | |
| |
| |
Using Thread.sleep() | |
| |
| |
Summary | |
| |
| |
| |
Implementing Runnable Versus Extending Thread | |
| |
| |
Visual Timer Graphical Component | |
| |
| |
Extending Thread and JComponent? | |
| |
| |
Interface java.lang.Runnable | |
| |
| |
Passing a Runnable Object to a Thread's Constructor | |
| |
| |
Modifying SecondCounter to Use Runnable | |
| |
| |
Checking the Accuracy of SecondCounter | |
| |
| |
Improving the Accuracy of SecondCounter | |
| |
| |
Summary | |
| |
| |
| |
Gracefully Stopping Threads | |
| |
| |
Interrupting a Thread: interrupt() | |
| |
| |
Suspending and Resuming Thread Execution | |
| |
| |
Stopping a Thread | |
| |
| |
The Best Replacement for stop(), suspend(), and resume() | |
| |
| |
Daemon Threads | |
| |
| |
Summary | |
| |
| |
| |
Thread Prioritization | |
| |
| |
System Thread Priorities | |
| |
| |
Thread Priority Constants | |
| |
| |
Determining the Current Priority: getPriority() | |
| |
| |
Changing the Priority of a Thread: setPriority() | |
| |
| |
Thread States | |
| |
| |
Priorities and Scheduling | |
| |
| |
Voluntarily Relinquishing the Processor: Thread.yield() | |
| |
| |
Thread-Scheduling Scenarios | |
| |
| |
Summary | |
| |
| |
| |
Concurrent Access to Objects and Variables | |
| |
| |
Volatile Member Variable Modifier | |
| |
| |
Synchronized Method Modifier | |
| |
| |
Synchronized Statement Block | |
| |
| |
Static synchronized Methods | |
| |
| |
Using the Class-Level Lock in a synchronized Statement | |
| |
| |
Synchronization and the Collections API | |
| |
| |
Deadlocks | |
| |
| |
Speeding Concurrent Access | |
| |
| |
Summary | |
| |
| |
| |
Inter-thread Communication | |
| |
| |
The Need for Inter-thread Signaling | |
| |
| |
The Wait/Notify Mechanism | |
| |
| |
Object API Used for Wait/Notify | |
| |
| |
When to Use notify All() Instead of notify() | |
| |
| |
Missed Notification | |
| |
| |
Early Notification | |
| |
| |
CubbyHole Example | |
| |
| |
Using join() to Wait for a Thread to Die | |
| |
| |
Streaming Data Between Threads Using Pipes | |
| |
| |
Using ThreadLocal and Inheritable ThreadLocal | |
| |
| |
Summary | |
| |
| |
| |
Threads and Swing | |
| |
| |
Why Isn't the Swing Toolkit Multithread-Safe? | |
| |
| |
Using Swing Utilities.invokeAndWait() | |
| |
| |
Using Swing Utilities.invokeLater() | |
| |
| |
Using SwingUtilities.isEventDispatch Thread() | |
| |
| |
When invokeAndWait() and invokeLater() Are Not Needed | |
| |
| |
The Need for Worker Threads in a GUI Setting | |
| |
| |
Using a Worker Thread to Relieve the Event Thread | |
| |
| |
Scrolling Text in a Custom Component | |
| |
| |
Animating a Set of Images | |
| |
| |
Displaying Elapsed Time on a JLabel | |
| |
| |
Floating Components Around Inside a Container | |
| |
| |
Summary | |
| |
| |
| |
Thread Groups | |
| |
| |
What Are Thread Groups? | |
| |
| |
Using getParent() | |
| |
| |
Finding the Subgroups of a Thread Group | |
| |
| |
Using the getThreadGroup() Method of Thread | |
| |
| |
Finding All the Threads in a Thread Group | |
| |
| |
Understanding Thread Group Security | |
| |
| |
Using setMaxPriority() and getMaxPriority() | |
| |
| |
Using interrupt() | |
| |
| |
Deprecated Methods: stop(), suspend(), and resume() | |
| |
| |
Class ThreadViewer | |
| |
| |
Summary | |
| |
| |
| |
Techniques | |
| |
| |
| |
Self-Running Objects | |
| |
| |
Simple Self-Running Class | |
| |
| |
Using an Inner Class to Hide run() | |
| |
| |
Additional Functionality to Consider | |
| |
| |
Summary | |
| |
| |
| |
Exception Callback | |
| |
| |
ExceptionListener Interface | |
| |
| |
Additional Methods to Support ExceptionListener | |
| |
| |
Summary | |
| |
| |
| |
Thread Pooling | |
| |
| |
Benefits of Thread Pooling | |
| |
| |
Considerations and Costs of Thread Pooling | |
| |
| |
A Generic Thread Pool: ThreadPool | |
| |
| |
A Specialized Worker Thread Pool: HttpServer | |
| |
| |
Summary | |
| |
| |
| |
Waiting for the Full Timeout | |
| |
| |
Accidentally Returning Early | |
| |
| |
Determining If wait() Should Be Invoked Again | |
| |
| |
A General-Purpose Wait-Until Pattern | |
| |
| |
Summary | |
| |
| |
| |
Breaking Out of a Blocked I/O State | |
| |
| |
The read() Method Ignores Interrupts and Stop Requests | |
| |
| |
Closing a Stream to Break Out of the Blocked State | |
| |
| |
Throwing InterruptedIOException When Interrupted | |
| |
| |
Using BufferedThreadedInputStream for Interruptible I/O | |
| |
| |
Summary | |
| |
| |
| |
The SureStop Utility | |
| |
| |
Guidelines for Using SureStop | |
| |
| |
The SureStop Class | |
| |
| |
Peering Inside Using SureStop Verbose | |
| |
| |
Watching It Work with SureStopDemo | |
| |
| |
Summary | |
| |
| |
| |
The BooleanLock Utility | |
| |
| |
Background | |
| |
| |
BooleanLock Class | |
| |
| |
Inter-Thread Signaling Using BooleanLock | |
| |
| |
Avoiding Blocking on synchronized | |
| |
| |
Noticing Brief Changes in Value Using TransitionDetector | |
| |
| |
Summary | |
| |
| |
| |
First-In-First-Out (FIFO) Queue | |
| |
| |
How a FIFO Queue Works | |
| |
| |
Implementing a FIFO with an Array | |
| |
| |
Simple Implementation in Java: SimpleObjectFIFO | |
| |
| |
An Expanded FIFO Queue for Object References: ObjectFIFO | |
| |
| |
A FIFO Queue for Bytes: ByteFIFO | |
| |
| |
Summary | |
| |
| |
| |
Appendixes | |
| |
| |
| |
The Thread API | |
| |
| |
Member Variables | |
| |
| |
Constructors | |
| |
| |
Static methods | |
| |
| |
Instance Methods | |
| |
| |
Deprecated Methods | |
| |
| |
| |
The ThreadGroup API | |
| |
| |
Constructors | |
| |
| |
Instance Methods | |
| |
| |
Deprecated Methods | |
| |
| |
Index | |