O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java Threads, 3rd Edition

Book Description

Threads are essential to Java programming, but learning to use them effectively is a nontrivial task. This new edition of the classic Java Threads shows you how to take full advantage of Java's threading facilities and brings you up-to-date with the watershed changes in Java 2 Standard Edition version 5.0 (J2SE 5.0). It provides a thorough, step-by-step approach to threads programming.Java's threading system is simple relative to other threading systems. In earlier versions of Java, this simplicity came with tradeoffs: some of the advanced features in other threading systems were not available in Java. J2SE 5.0 changes all that: it provides a large number of new thread-related classes that make the task of writing multithreaded programs that much easier.You'll learn where to use threads to increase efficiency, how to use them effectively, and how to avoid common mistakes. This book discusses problems like deadlock, race conditions, and starvation in detail, helping you to write code without hidden bugs.Java Threads, Third Edition, has been thoroughly expanded and revised. It incorporates the concurrency utilities from java.util.concurrent throughout. New chapters cover thread performance, using threads with Swing, threads and Collection classes, thread pools, and threads and I/O (traditional, new, and interrupted). Developers who cannot yet deploy J2SE 5.0 can use thread utilities provided in the Appendix to achieve similar functionality with earlier versions of Java.Topics include:

  • Lock starvation and deadlock detection
  • Atomic classes and minimal synchronization (J2SE 5.0)
  • Interaction of Java threads with Swing, I/O, and Collection classes
  • Programmatically controlled locks and condition variables (J2SE 5.0)
  • Thread performance and security
  • Thread pools (J2SE 5.0)
  • Thread groups
  • Platform-specific thread scheduling
  • Task schedulers (J2SE 5.0)
  • Parallelizing loops for multiprocessor machines
In short, this new edition of Java Threads covers everything you need to know about threads, from the simplest animation program to the most complex applications. If you plan to do any serious work in Java, you will find this book invaluable.Scott Oaks is a senior software engineer for the Java Performance Engineering group at Sun Microsystems and the author of four books in the O'Reilly Java series.Formerly a senior systems engineer at Sun Microsystems, Henry Wong is an independent consultant working on various Java related projects.

Table of Contents

  1. A Note Regarding Supplemental Files
  2. Preface
    1. Who Should Read This Book?
    2. Versions Used in This Book
    3. What’s New in This Edition?
    4. Organization of This Book
    5. Conventions Used in This Book
    6. Code Examples
    7. How to Contact Us
    8. Safari Enabled
    9. Acknowledgments
  3. 1. Introduction to Threads
    1. 1.1. Java Terms
      1. 1.1.1. Java Versions, Tools, and Code
    2. 1.2. About the Examples
      1. 1.2.1. Compiling and Running the Examples
    3. 1.3. Why Threads?
      1. 1.3.1. Nonblocking I/O
      2. 1.3.2. Alarms and Timers
      3. 1.3.3. Independent Tasks
      4. 1.3.4. Parallelizable Algorithms
    4. 1.4. Summary
  4. 2. Thread Creation and Management
    1. 2.1. What Is a Thread?
    2. 2.2. Creating a Thread
      1. 2.2.1. The Example Architecture
      2. 2.2.2. The Thread Class
    3. 2.3. The Lifecycle of a Thread
      1. 2.3.1. Creating a Thread
      2. 2.3.2. Starting a Thread
      3. 2.3.3. Terminating a Thread
      4. 2.3.4. Pausing, Suspending, and Resuming Threads
      5. 2.3.5. Thread Cleanup
    4. 2.4. Two Approaches to Stopping a Thread
      1. 2.4.1. Setting a Flag
      2. 2.4.2. Interrupting a Thread
    5. 2.5. The Runnable Interface
    6. 2.6. Threads and Objects
      1. 2.6.1. Determining the Current Thread
    7. 2.7. Summary
      1. 2.7.1. Example Classes
  5. 3. Data Synchronization
    1. 3.1. The Synchronized Keyword
    2. 3.2. The Volatile Keyword
    3. 3.3. More on Race Conditions
    4. 3.4. Explicit Locking
    5. 3.5. Lock Scope
      1. 3.5.1. Synchronized Blocks
    6. 3.6. Choosing a Locking Mechanism
      1. 3.6.1. The Lock Interface
    7. 3.7. Nested Locks
    8. 3.8. Deadlock
    9. 3.9. Lock Fairness
    10. 3.10. Summary
      1. 3.10.1. Example Classes
  6. 4. Thread Notification
    1. 4.1. Wait and Notify
      1. 4.1.1. The Wait-and-Notify Mechanism and Synchronization
      2. 4.1.2. wait( ), notify( ), and notifyAll( )
      3. 4.1.3. Wait-and-Notify Mechanism with Synchronized Blocks
    2. 4.2. Condition Variables
    3. 4.3. Summary
      1. 4.3.1. Example Classes
  7. 5. Minimal Synchronization Techniques
    1. 5.1. Can You Avoid Synchronization?
      1. 5.1.1. The Effect of Registers
      2. 5.1.2. The Effect of Reordering Statements
      3. 5.1.3. Double-Checked Locking
    2. 5.2. Atomic Variables
      1. 5.2.1. Overview of the Atomic Classes
      2. 5.2.2. Using the Atomic Classes
        1. 5.2.2.1. Variable substitution
        2. 5.2.2.2. Changing algorithms
        3. 5.2.2.3. Retrying operations
      3. 5.2.3. Notifications and Atomic Variables
      4. 5.2.4. Summary of Atomic Variable Usage
        1. 5.2.4.1. Data exchange
        2. 5.2.4.2. Compare and set
        3. 5.2.4.3. Advanced atomic data types
        4. 5.2.4.4. Bulk data modification
    3. 5.3. Thread Local Variables
      1. 5.3.1. Inheritable Thread Local Variables
    4. 5.4. Summary
      1. 5.4.1. Example Classes
  8. 6. Advanced Synchronization Topics
    1. 6.1. Synchronization Terms
    2. 6.2. Synchronization Classes Added in J2SE 5.0
      1. 6.2.1. Semaphore
      2. 6.2.2. Barrier
      3. 6.2.3. Countdown Latch
      4. 6.2.4. Exchanger
      5. 6.2.5. Reader/Writer Locks
    3. 6.3. Preventing Deadlock
      1. 6.3.1. Deadlock and Automatic Lock Releases
      2. 6.3.2. Preventing Deadlock with Timeouts
    4. 6.4. Deadlock Detection
    5. 6.5. Lock Starvation
      1. 6.5.1. Lock Starvation and Reader/Writer Locks
    6. 6.6. Summary
      1. 6.6.1. Example Classes
  9. 7. Threads and Swing
    1. 7.1. Swing Threading Restrictions
    2. 7.2. Processing on the Event-Dispatching Thread
    3. 7.3. Using invokeLater( ) and invokeAndWait( )
    4. 7.4. Long-Running Event Callbacks
    5. 7.5. Summary
      1. 7.5.1. Example Classes
  10. 8. Threads and Collection Classes
    1. 8.1. Overview of Collection Classes
      1. 8.1.1. Collection Interfaces
      2. 8.1.2. Threadsafe Collection Classes
      3. 8.1.3. Thread-Unsafe Collection Classes
      4. 8.1.4. Thread-Notification Collection Classes
    2. 8.2. Synchronization and Collection Classes
      1. 8.2.1. Simple Synchronization
      2. 8.2.2. Complex Synchronization
      3. 8.2.3. Iterators and Enumerations
      4. 8.2.4. Thread-Aware Classes
    3. 8.3. The Producer/Consumer Pattern
    4. 8.4. Using the Collection Classes
    5. 8.5. Summary
      1. 8.5.1. Example Classes
  11. 9. Thread Scheduling
    1. 9.1. An Overview of Thread Scheduling
      1. 9.1.1. Priority-Based Scheduling
      2. 9.1.2. The Scheduling Process
      3. 9.1.3. Priority Exceptions
        1. 9.1.3.1. Priority inversion
        2. 9.1.3.2. Complex priorities
    2. 9.2. Scheduling with Thread Priorities
      1. 9.2.1. Other Thread-Scheduling Methods
    3. 9.3. Popular Threading Implementations
      1. 9.3.1. Green Threads
      2. 9.3.2. Windows Native Threads
      3. 9.3.3. Solaris Native Threads
      4. 9.3.4. Linux Native Threads
    4. 9.4. Summary
      1. 9.4.1. Example Classes
  12. 10. Thread Pools
    1. 10.1. Why Thread Pools?
      1. 10.1.1. Thread Pools and Throughput
      2. 10.1.2. Why Not Thread Pools?
    2. 10.2. Executors
    3. 10.3. Using a Thread Pool
    4. 10.4. Queues and Sizes
      1. 10.4.1. Rejected Tasks
    5. 10.5. Thread Creation
    6. 10.6. Callable Tasks and Future Results
      1. 10.6.1. The FutureTask Class
    7. 10.7. Single-Threaded Access
    8. 10.8. Summary
      1. 10.8.1. Example Classes
  13. 11. Task Scheduling
    1. 11.1. Overview of Task Scheduling
    2. 11.2. The java.util.Timer Class
      1. 11.2.1. Using the Timer
    3. 11.3. The javax.swing.Timer Class
      1. 11.3.1. Using the javax.swing.Timer Class
    4. 11.4. The ScheduledThreadPoolExecutor Class
      1. 11.4.1. Using the ScheduledThreadPoolExecutor Class
      2. 11.4.2. Using the Future Interface
    5. 11.5. Summary
      1. 11.5.1. Example Classes
  14. 12. Threads and I/O
    1. 12.1. A Traditional I/O Server
      1. 12.1.1. An Example Multithreaded Server
        1. 12.1.1.1. Using the multithreaded server
      2. 12.1.2. Scaling Using Traditional I/O
    2. 12.2. A New I/O Server
      1. 12.2.1. Nonblocking I/O
      2. 12.2.2. A Single-Threaded NIO Server
      3. 12.2.3. A Multithreaded New I/O Server
    3. 12.3. Interrupted I/O
    4. 12.4. Summary
      1. 12.4.1. Example Classes
  15. 13. Miscellaneous Thread Topics
    1. 13.1. Thread Groups
    2. 13.2. Threads and Java Security
    3. 13.3. Daemon Threads
    4. 13.4. Threads and Class Loading
    5. 13.5. Threads and Exception Handling
      1. 13.5.1. The ThreadDeath Class
    6. 13.6. Threads, Stacks, and Memory Usage
      1. 13.6.1. Stack Overflow Errors
      2. 13.6.2. Out of Memory Errors
      3. 13.6.3. Specifying Stack Sizes
      4. 13.6.4. Stack APIs
    7. 13.7. Summary
      1. 13.7.1. Example Classes
  16. 14. Thread Performance
    1. 14.1. Overview of Performance
      1. 14.1.1. Measuring Java Performance
    2. 14.2. Synchronized Collections
    3. 14.3. Atomic Variables and Contended Synchronization
      1. 14.3.1. The ConcurrentHashMap Class
    4. 14.4. Thread Creation and Thread Pools
    5. 14.5. Summary
      1. 14.5.1. Example Classes
  17. 15. Parallelizing Loops for Multiprocessor Machines
    1. 15.1. Parallelizing a Single-Threaded Program
      1. 15.1.1. Loop Scheduling and Load Balancing
        1. 15.1.1.1. Static or chunk scheduling
        2. 15.1.1.2. Self-scheduling
        3. 15.1.1.3. Guided self-scheduling
        4. 15.1.1.4. User-defined scheduling
      2. 15.1.2. Variable Classifications
        1. 15.1.2.1. Loop-private variables
        2. 15.1.2.2. Read-only variables
        3. 15.1.2.3. Storeback variables
        4. 15.1.2.4. Reduction variables
        5. 15.1.2.5. Shared variables
      3. 15.1.3. Loop Analysis and Transformations
        1. 15.1.3.1. Loop distribution
        2. 15.1.3.2. Loop isolation
        3. 15.1.3.3. Loop interchange
        4. 15.1.3.4. Loop reimplementation
      4. 15.1.4. Inner-Loop Threading
      5. 15.1.5. Loop Printing
    2. 15.2. Multiprocessor Scaling
      1. 15.2.1. A Simple Loop Test
      2. 15.2.2. A Reduction Variable Test
      3. 15.2.3. A Small Inner-Loop Test
      4. 15.2.4. A Printing Test
    3. 15.3. Summary
      1. 15.3.1. Example Classes
  18. A. Superseded Threading Utilities
    1. A.1. The BusyFlag Class
    2. A.2. The CondVar Class
    3. A.3. The Barrier Class
    4. A.4. The RWLock Class
    5. A.5. The ThreadPool Class
    6. A.6. The JobScheduler Class
      1. A.6.1. The DaemonLock Class
    7. A.7. Summary
  19. Index
  20. About the Authors
  21. Colophon
  22. Copyright