Multithreading gives developers using the Java 2 platform a powerful tool for dramatically improving the responsiveness and performance of their program on any platform, even those without inherent multithreading support.
Multithreaded Programming with Java Technology is the first complete guide to multithreaded development with the Java 2 platform. Multithreading experts Bil Lewis and Daniel J. Berg cover the underlying structures upon which threads are built; thread construction, and thread lifecycles, including birth, life, death, and cancellation. Next, using extensive code examples, they cover everything developers need to know to make the most of multithreading.
1. Introduction.
2. Concepts.
Background: Traditional Operating Systems. What Is a Thread? Kernel Interaction. Concurrency vs Parallelism. System Calls. Signals. Synchronization. Scheduling. The Value of Using Threads. Parallelism. Throughput. Responsiveness. Communications. System Resources. Distributed Objects. Same Binary for Uniprocessors and Multiprocessors. Program Structure. What Kinds of Programs to Thread. Inherently MT Programs. Not Obviously MT Programs. Automatic Threading. Programs Not to Thread. What About Shared Memory? Threads Standards. Performance. Operating Systems. NFS. SPECfp 95. SPECint_rate95. Java Benchmarks.
3. Foundations.
Implementation vs Specification. Thread Libraries. The Process Structure. Lightweight Processes. Threads and LWPs. The POSIX Multithreaded Model. System Calls. Signals.
4. Lifecycle.
Thread Lifecycle. Exiting a Thread. The Runnable Interface. Waiting for Threads. Who Am I? Exiting the Process. Suspending a Thread. Cancellation. ThreadDeath. Garbage Collecting Threads. Zombies. Is She Still Alive? Restarting Threads. An Example: Create and Join. APIs Used in This Chapter. The Class java.lang.Thread. The Class Extensions.InterruptibleThread. The Interface java.lang.Runnable.
5. Scheduling.
Different Models of Kernel Scheduling. Many Threads on One LWP. One Thread per LWP. Many Threads on Many LWPs (Strict). The Two-Level Model. Thread Scheduling. Process Contention Scope. System Contention Scope. Context Switching. Preemption. How Many LWPs? How to Get Those LWPs in Java. Changing Scheduling Parameters for LWPs. Realtime LWPs. Allocation Domains. Binding LWPs to Processors. Java Scheduling Summary. When Should You Care About Scheduling? APIs Used in This Chapter. The Class java.lang.Thread.
6. Synchronization.
Synchronization Issues. Atomic Actions and Atomic Instructions. Critical Sections. Lock Your Shared Data! Synchronization Variables. Mutexes. Semaphores. Condition Variables. Java wait/notify. InterruptedException. Controlling the Queue Length. POSIX-Style Synchronization in Java. APIs Used in This Chapter. The Class java.lang.Object. The Class Extensions.Semaphore. The Class Extensions.Mutex. The Class Extensions.ConditionVar.
7. Complexities.
Complex Locking Primitives. Readers/Writer Locks. Priority Inheritance Mutexes. FIFO Mutexes. Recursive Mutexes. Nonblocking Synchronization. Spin Locks. Timeouts. Elvis and the UFOs. Other Synchronization Variables. Join. Barriers. Single Barriers. Win32 Event Objects. Win32 Critical Sections. Multiple Wait Semaphores. Interlocked Instructions. Message Queues. Win32 I/O Completion Ports. Communicating via Streams. Volatile. Performance. Condition Variables vs wait/notify. Coarse vs Fine Grain Locking. What to Lock. Double-Checked Locking. Synchronization Problems. Deadlocks. Race Conditions. Recovering from Deadlocks. The Lost Wakeup. InterruptedException. APIs Used in This Chapter. The Class Extensions.RWLock. The Class Extensions.Barrier. The Class Extensions.SingleBarrier.
8. TSD.
Thread-Specific Data. Java TSD. APIs Used in This Chapter. The Class java.lang.ThreadLocal.
9. Cancellation.
What Cancellation Is. Polling for Cancellation. Asynchronous Cancellation. Deferred Cancellation. Using interrupt() for Deferred Cancellation. Progressive Shutdown. interrupt( ). Don't Call stop(). ThreadDeath. Using stop() to Implement Thread.exit(). Never Exit a Thread! Defined Cancellation/Interruption Points. Not Cancelling upon Interruption. Handling Interrupts. Cancellation State. A Cancellation Example. Using Cancellation. Ensuring Bounded CPU Time. Interrupting Sleeping Threads. The Morning After. Cleanup. Implementing enableInterrupts( ). A Cancellation Example (Improved). Simple Polling. APIs Used in This Chapter. The Class java.lang.Thread. The Class Extensions.InterruptibleThread.
10. Details.
Thread Groups. Thread Security. Real-World Examples. General Tips and Hints. Daemon Threads. Daemon Thread Groups. Calling Native Code. A Few Assorted Methods. Stack Size. Deprecated Methods. The Effect of Using a JIT. Adaptive Compilers. APIs Used in This Chapter. The Class java.lang.Thread. The Class java.lang.ThreadGroup.
11. Libraries.
The Native Threads Libraries. Multithreaded Kernels. Symmetric Multiprocessing. Are Libraries Safe? Window Systems. Working with Unsafe Libraries. When Should a Class Be Synchronized? Synchronized Collections in Java 2. Java's Multithreaded Garbage Collector.
12. Design.
Making Libraries Safe and Hot. Making malloc() More Concurrent. Manipulating Lists. Single, Global Mutex. Global RWLock with Global Mutex to Protect Salaries. Global RWLock with Local Mutex to Protect Salaries. One Local Lock. Two Local Locks. Local RWLock with Local Mutex to Protect Salaries. Program Design. Design Patterns.
13. RMI.
Remote Method Invocation. Sending Remote References. RMI's Use of Threads. The Deadlock Problem with RMI. Remote Garbage Collection.
14. Tools.
Static Lock Analyzer. Using a Thread-Aware, Graphical Debugger. Proctool. TNFview.
15. Performance.
Optimization: Objectives and Objections. CPU Time, I/O Time, Contention, Etc. CPU. Memory Latency. Memory Bandwidth. I/O Latency. Contention. Throughput vs Latency. Limits on Speedup. Amdahl's Law. Performance Bottlenecks. Benchmarks and Repeatable Testing. Is It Really Faster? General Performance Optimizations. Thread-Specific Performance Optimizations. Dealing with Many Open Sockets. The Lessons of NFS.
16. Hardware.
Types of Multiprocessors. Shared Memory Symmetric Multiprocessors. Bus Architectures. LoadLocked/StoreConditional and Compare and Swap. Volatile: The Rest of the Story. Memory Systems. Reducing Cache Misses.
17. Examples.
Threads and Windows. Displaying Things for a Moment (Memory.java). Socket Server (Master/Slave Version). Socket Server (Producer/Consumer Version). Making a Native Call to pthread_setconcurrency( ). Actual Implementation of POSIX Synchronization. A Robust, Interruptible Server. Disk Performance with Java. Other Programs on the Web.
Appendix A: Internet.
Threads Newsgroup. Code Examples. Vendor's Threads Pages. Threads Research. Freeware Tools. Other Pointers. The Authors on the Net.
Appendix B: Books.
Threads Books. Related Books.
Appendix C: Timings. Appendix D: APIs.
Function Descriptions. The Class java.lang.Thread. The Interface java.lang.Runnable. The Class java.lang.Object. The Class java.lang.ThreadLocal. The Class java.lang.ThreadGroup. Helper Classes from Our Extensions Library. The Class Extensions.InterruptibleThread. The Class Extensions.Semaphore. The Class Extensions.Mutex. The Class Extensions.ConditionVar. The Class Extensions.RWLock. The Class Extensions.Barrier. The Class Extensions.SingleBarrier. Glossary.
Index.