Multithreaded Programming with JAVA™ Technology

Book description


1700G-6

  • The ultimate guide to multithreading with Java technology!

  • Powerful techniques for enhancing application performance

  • Multithreaded program design for network and Internet applications

  • Extensive code examples throughout

  • Multithreading gives developers using the Java 2 platform a powerful tool for dramatically improving the responsiveness and performance of their programs 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, including:

  • Thread scheduling models and synchronization-with solutions for complex, real-world synchronization problems

  • Multithreaded program design for networked and Internet applications

  • Thread-specific data: use and implementation

  • Leveraging OS libraries to make Java-based multithreading more effective

  • Optimizing thread performance and designing for SMP hardware

  • Powerful techniques and comprehensive example code for improving Java-based application performance with multithreading!

    Table of contents

    1. Copyright
      1. Dedication
    2. Preface
      1. Who Should Use This Book
      2. How This Book Is Organized
    3. Acknowledgments
      1. Acknowledgments to the Threads Primer
      2. Acknowledgments to the Pthreads Primer
    4. 1. Introduction
    5. 2. Concepts
      1. Background: Traditional Operating Systems
      2. What Is a Thread?
      3. Kernel Interaction
        1. Concurrency vs. Parallelism
        2. System Calls
        3. Signals
        4. Synchronization
        5. Scheduling
      4. The Value of Using Threads
        1. Parallelism
        2. Throughput
        3. Responsiveness
        4. Communications
        5. System Resources
        6. Distributed Objects
        7. Same Binary for Uniprocessors and Multiprocessors
        8. Program Structure
      5. What Kinds of Programs to Thread
        1. Inherently MT Programs
          1. Independent tasks
          2. Servers
          3. Repetitive tasks
        2. Not Obviously MT Programs
          1. Numerical programs
          2. Old code
        3. Automatic Threading
        4. Programs Not to Thread
      6. What About Shared Memory?
      7. Threads Standards
        1. POSIX Threads
        2. Win32 and OS/2 Threads
        3. DCE Threads
        4. Solaris Threads
      8. Performance
        1. Operating Systems
        2. NFS
        3. SPECfp 95
        4. SPECint_rate95
        5. Java Benchmarks
      9. Summary
    6. 3. Foundations
      1. Implementation vs. Specification
      2. Thread Libraries
      3. The Process Structure
      4. Lightweight Processes
        1. Digital UNIX
        2. Linux
      5. Threads and LWPs
      6. The POSIX Multithreaded Model
      7. System Calls
      8. Signals
      9. Summary
    7. 4. Lifecycle
      1. Thread Lifecycle
        1. Exiting a Thread
        2. The Runnable Interface
        3. Waiting for Threads
        4. Who Am I?
          1. Don't Wait for Threads, Don't Return Status
        5. Exiting the Process
        6. Suspending a Thread
        7. Cancellation
        8. ThreadDeath
        9. Garbage Collecting Threads
        10. Zombies
        11. Is She Still Alive?
        12. Restarting Threads
        13. An Example: Create and Join
          1. The Main Thread Is Different
      2. APIs Used in This Chapter
        1. The Class java.lang.Thread
        2. The Class Extensions.InterruptibleThread
        3. The Interface java.lang.Runnable
      3. Summary
    8. 5. Scheduling
      1. Different Models of Kernel Scheduling
        1. Many Threads on One LWP
        2. One Thread per LWP
        3. Many Threads on Many LWPs (Strict)
        4. The Two-Level Model
          1. Win32 Fibers
      2. Thread Scheduling
        1. Process Contention Scope
          1. Priority Levels
          2. Scheduling States
            1. Active:
            2. Runnable:
            3. Sleeping:
            4. Suspended:
            5. Zombie:
        2. System Contention Scope
      3. Context Switching
        1. Preemption
        2. How Many LWPs?
        3. How to Get Those LWPs in Java
        4. Changing Scheduling Parameters for LWPs
          1. nice()
        5. Realtime LWPs
          1. Avoid Realtime
        6. Allocation Domains
        7. Binding LWPs to Processors
          1. Happiness Is a Warm Cache
      4. Java Scheduling Summary
        1. How Many Threads in Java?
      5. When Should You Care About Scheduling?
      6. APIs Used in This Chapter
        1. The Class java.lang.Thread
      7. Summary
    9. 6. Synchronization
      1. Synchronization Issues
        1. Atomic Actions and Atomic Instructions
        2. Critical Sections
        3. Lock Your Shared Data!
      2. Synchronization Variables
        1. Mutexes
        2. Semaphores
          1. Using Barriers to Count Exiting Threads
          2. A Different View of Semaphores
        3. Condition Variables
        4. Java wait/notify
          1. Extraneous Contention
        5. InterruptedException
        6. Controlling the Queue Length
        7. POSIX-Style Synchronization in Java
          1. POSIX-Style Mutexes in Java
          2. POSIX-Style Condition Variables in Java
          3. A Stoppable Producer/Consumer Example
      3. APIs Used in This Chapter
        1. The Class java.lang.Object
        2. The Class Extensions.Semaphore
        3. The Class Extensions.Mutex
        4. The Class Extensions.ConditionVar
      4. Summary
    10. 7. Complexities
      1. Complex Locking Primitives
        1. Readers/Writer Locks
        2. Priority Inheritance Mutexes
        3. FIFO Mutexes
        4. Recursive Mutexes
        5. Nonblocking Synchronization
        6. Spin Locks
          1. Adaptive Spin Locks
          2. Java May Use Spin Locks
      2. Timeouts
        1. Elvis and the UFOs
      3. Other Synchronization Variables
        1. Join
        2. Barriers
        3. Single Barriers
        4. Win32 Event Objects
        5. Win32 Critical Sections
        6. Multiple Wait Semaphores
        7. Interlocked Instructions
        8. Message Queues
        9. Win32 I/O Completion Ports
        10. Communicating via Streams
      4. Volatile
      5. Performance
        1. Condition Variables vs. wait/notify
        2. Coarse vs. Fine Grain Locking
        3. What to Lock
        4. Double-Checked Locking
      6. Synchronization Problems
        1. Deadlocks
        2. Race Conditions
        3. Recovering from Deadlocks
        4. The Lost Wakeup
        5. InterruptedException
      7. APIs Used in This Chapter
        1. The Class Extensions.RWLock
        2. The Class Extensions.Barrier
        3. The Class Extensions.SingleBarrier
      8. Summary
    11. 8. TSD
      1. Thread-Specific Data
      2. Java TSD
      3. APIs Used in This Chapter
        1. The Class java.lang.ThreadLocal
      4. Summary
    12. 9. Cancellation
      1. What Cancellation Is
        1. Polling for Cancellation
        2. Asynchronous Cancellation
        3. Deferred Cancellation
        4. Using interrupt() for Deferred Cancellation
        5. Progressive Shutdown
      2. interrupt()
        1. Don't Call stop()
        2. ThreadDeath
        3. Using stop() to Implement Thread.exit()
        4. Never Exit a Thread!
          1. Don't Call destroy()
        5. Defined Cancellation/Interruption Points
          1. The Problem with I/O
        6. Not Cancelling upon Interruption
        7. Handling Interrupts
          1. Disabling Interrupts
          2. Ignore Interrupts
          3. Exit on interrupt()
          4. Propagate InterruptedException
          5. Reinterrupt
        8. Cancellation State
      3. A Cancellation Example
      4. Using Cancellation
        1. Ensuring Bounded CPU Time
          1. Interrupting Computational Loops
          2. Interrupting Blocked Threads
          3. Interrupting Sockets
          4. What Should Throw InterruptedException?
        2. Interrupting Sleeping Threads
          1. Interruption in wait()
        3. The Morning After
      5. Cleanup
      6. Implementing enableInterrupts()
      7. A Cancellation Example (Improved)
      8. Simple Polling
      9. APIs Used in This Chapter
        1. The Class java.lang.Thread
        2. The Class Extensions.InterruptibleThread
      10. Summary
    13. 10. Details
      1. Thread Groups
      2. Thread Security
        1. Real-World Examples
          1. 1. The garbage collector thread and the finalize() method
          2. 2. Performance: Access to synchronized objects
          3. 3. More problems with the garbage collection thread
          4. 4. Make synchronized code sections as small as possible
          5. 5. Threaded class downloads
        2. General Tips and Hints
      3. Daemon Threads
      4. Daemon Thread Groups
      5. Calling Native Code
      6. A Few Assorted Methods
        1. Stack Size
      7. Deprecated Methods
      8. The Effect of Using a JIT
        1. Adaptive Compilers
      9. APIs Used in This Chapter
        1. The Class java.lang.Thread
        2. The Class java.lang.ThreadGroup
      10. Summary
    14. 11. Libraries
      1. The Native Threads Libraries
      2. Multithreaded Kernels
        1. Symmetric Multiprocessing
      3. Are Libraries Safe?
        1. Window Systems
        2. Working with Unsafe Libraries
        3. When Should a Class Be Synchronized?
        4. Synchronized Collections in Java 2
      4. Java's Multithreaded Garbage Collector
        1. Locks during Finalization
      5. Summary
    15. 12. Design
      1. Making Libraries Safe and Hot
        1. Trivial Library Functions
        2. Functions That Maintain State across Invocations
        3. Making malloc() More Concurrent
          1. Using Thread-Specific Data to Make malloc() More Concurrent
          2. Using Other Methods to Make malloc() More Concurrent
        4. Manipulating Lists
          1. Basic Design
        5. Single, Global Mutex
        6. Global RWLock with Global Mutex to Protect Salaries
        7. Global RWLock with Local Mutex to Protect Salaries
        8. One Local Lock
        9. Two Local Locks
        10. Local RWLock with Local Mutex to Protect Salaries
      2. Program Design
        1. Master/Slave
        2. Client/Server (Thread per Request)
        3. Producer/Consumer
        4. Pipeline
        5. Client/Server (Thread per Client)
      3. Design Patterns
      4. Summary
    16. 13. RMI
      1. Remote Method Invocation
        1. Sending Remote References
        2. RMI's Use of Threads
        3. The Deadlock Problem with RMI
        4. Remote Garbage Collection
      2. Summary
    17. 14. Tools
      1. Static Lock Analyzer
      2. Using a Thread-Aware, Graphical Debugger
      3. Proctool
      4. TNFview
      5. Summary
    18. 15. Performance
      1. Optimization: Objectives and Objections
        1. Time to Market
        2. Available Human Resources and Programming Costs
        3. Portability
        4. User Perception
        5. Competition
        6. Targeted Machine Configuration
        7. Algorithm
      2. CPU Time, I/O Time, Contention, Etc.
        1. CPU
        2. Memory Latency
        3. Memory Bandwidth
        4. I/O Latency
        5. Contention
        6. Throughput vs. Latency
      3. Limits on Speedup
        1. Superlinear Speedup
        2. Timing Threaded and Nonthreaded Programs
      4. Amdahl's Law
      5. Performance Bottlenecks
      6. Benchmarks and Repeatable Testing
        1. Is It Really Faster?
        2. General Performance Optimizations
          1. Best Algorithm
          2. Compiler Optimization
            1. C Compiler Optimization
            2. Java Compiler Optimization
          3. Buy Enough RAM
          4. Minimize I/O
          5. Minimize Cache Misses
          6. Any Other Loop Optimizations
        3. Thread-Specific Performance Optimizations
          1. Reducing Contention
          2. Minimizing MT Overhead
          3. Reducing Paging
          4. Communications Bandwidth
          5. Right Number of Threads
          6. Short-Lived Threads
        4. Dealing with Many Open Sockets
      7. The Lessons of NFS
      8. Summary
    19. 16. Hardware
      1. Types of Multiprocessors
        1. Shared Memory Symmetric Multiprocessors
          1. The CPU
          2. The System
          3. Store Barriers
      2. Bus Architectures
        1. Direct-Switched Buses
        2. Packet-Switched Buses
        3. Crossbar Switches
        4. Hierarchical Interconnects
        5. ccNUMA
        6. Packet-Switched Buses and ldstub
        7. The Thundering Herds
        8. LoadLocked/StoreConditional and Compare and Swap
          1. Lock-Free Semaphores and Reference Counting
        9. Volatile: The Rest of the Story
          1. Atomic Reads and Writes
          2. Interlocked Instructions
      3. Memory Systems
        1. Reducing Cache Misses
          1. Cache Blocking
          2. Data Reorganization
          3. Word Tearing
          4. False Sharing
      4. Summary
    20. 17. Examples
      1. Threads and Windows
      2. Displaying Things for a Moment (Memory.java)
      3. Socket Server (Master/Slave Version)
      4. Socket Server (Producer/Consumer Version)
      5. Making a Native Call to pthread_setconcurrency()
      6. Actual Implementation of POSIX Synchronization
      7. A Robust, Interruptible Server
      8. Disk Performance with Java
      9. Other Programs on the Web
      10. Summary
    21. A. Internet
      1. Threads Newsgroup
      2. Code Examples
      3. Vendor's Threads Pages
      4. Threads Research
      5. Freeware Tools
      6. Other Pointers
      7. The Authors on the Net
    22. B. Books
      1. Threads Books
        1. Java Threads
          1. Bibliography
        2. POSIX Threads
          1. Bibliography
        3. Win32 Threads
          1. Bibliography
      2. Related Books
        1. Bibliography
    23. C. Timings
      1. Timings
        1. Mutex Lock/Unlock
        2. Explicit Synchronized
        3. Implicit Synchronized
        4. Readers/Writer Lock/Unlock
        5. Semaphore Post/Wait
        6. notify()
        7. condSignal()
        8. Local Context Switch (unbound)
        9. Local Context Switch (bound)
        10. Process Context Switch
        11. Cancellation Disable/Enable
        12. Test for Deferred Cancellation
        13. Reference a Global Variable
        14. Reference Thread-Specific Data
        15. Reference “Fake” Thread-Specific Data
    24. D. APIs
      1. Function Descriptions
      2. The Class java.lang.Thread
      3. The Interface java.lang.Runnable
      4. The Class java.lang.Object
      5. The Class java.lang.ThreadLocal
      6. The Class java.lang.ThreadGroup
      7. Helper Classes from Our Extensions LibraryThe Class Extensions.InterruptibleThread
      8. The Class Extensions.Semaphore
      9. The Class Extensions.Mutex
      10. The Class Extensions.ConditionVar
      11. The Class Extensions.RWLock
      12. The Class Extensions.Barrier
      13. The Class Extensions.SingleBarrier
    25. Glossary

    Product information

    • Title: Multithreaded Programming with JAVA™ Technology
    • Author(s): Bil Lewis, Daniel J. Berg
    • Release date: December 1999
    • Publisher(s): Pearson
    • ISBN: 0130170070