Java Threads, Second Edition

Book description

Threads aren't a new idea: many operating systems and languages support them. But despite widespread support, threads tend to be something that everyone talks about, but few use. Programming with threads has a reputation for being tricky and nonportable. Not so with Java. Java's thread facilities are easy to use, and--like everything else in Java--are completely portable between platforms. And that's a good thing, because it's impossible to write anything but the simplest applet without encountering threads. If you want to work with Java, you have to learn about threads. This new edition shows you how to take full advantage of Java's thread facilities: where to use threads to increase efficiency, how to use them effectively, and how to avoid common mistakes. Java Threads discusses problems like deadlock, race condition, and starvation in detail, helping you to write code without hidden bugs. It brings you up to date with the latest changes in the thread interface for JDK 1.2. The book offers a thorough discussion of the Thread and ThreadGroup classes, the Runnable interface, the language's synchronized operator. It explains thread scheduling ends by developing a CPUSchedule class, showing you how to implement your own scheduling policy. In addition, Java Threads shows you how to extend Java's thread primitives. Other extended examples include classes that implement reader/writer locks, general locks, locks at arbitrary scope, and asynchronous I/O. This edition also adds extensive examples on thread pools, advanced synchronization technique, like condition variables, barriers, and daemon locks. It shows how to work with classes that are not thread safe, and pays special attention to threading issues with Swing. A new chapter shows you how to write parallel code for multiprocessor machines. In short, Java Threads covers everything you need to know about threads, from the simplest animation applet to the most complex applications. If you plan to do any serious work in Java, you will find this book invaluable. Examples available online. Covers Java 2.

Publisher resources

Download Example Code

Table of contents

  1. Java Threads, 2nd Edition
    1. Preface
      1. Who Should Read This Book?
      2. Versions Used in This Book
      3. Organization of This Book
      4. Conventions Used in This Book
      5. Feedback for Authors
      6. Acknowledgments
    2. 1. Introduction to Threading
      1. Java Terms
      2. Thread Overview
        1. Overview of Multitasking
        2. Overview of Multithreading
      3. Why Threads?
        1. Nonblocking I/O
        2. Alarms and Timers
        3. Independent Tasks
        4. Parallelizable Algorithms
      4. Summary
    3. 2. The Java ThreadingAPI
      1. Threading Using the Thread Class
        1. Animate Applet
        2. Stopping a Thread
      2. Threading Using the Runnable Interface
      3. The Life Cycle of a Thread
        1. The isAlive() Method
        2. Joining Threads
      4. Thread Naming
      5. Thread Access
        1. The Current Thread
        2. Enumerating Threads in the Virtual Machine
      6. More on Starting, Stopping, and Joining
      7. Summary
    4. 3. Synchronization Techniques
      1. A Banking Example
      2. Reading Data Asynchronously
      3. A Class to Perform Synchronization
      4. The Synchronized Block
      5. Nested Locks
      6. Deadlock
      7. Return to the Banking Example
      8. Synchronizing Static Methods
      9. Summary
    5. 4. Wait and Notify
      1. Back to Work (at the Bank)
      2. Wait and Notify
      3. wait(), notify(), and notifyAll()
      4. wait() and sleep()
      5. Thread Interruption
        1. Interrupted I/O
      6. Static Methods (Synchronization Details)
      7. Summary
    6. 5. Useful Examplesof Java Thread Programming
      1. Data Structures and Containers
        1. The Message Queue
        2. Shared Memory
        3. The Circular Linked List
        4. Synchronization and Efficiency
      2. Simple Synchronization Examples
        1. Barrier
        2. Condition Variables
      3. A Network Server Class
      4. The AsyncInputStream Class
      5. Using TCPServer with AsyncInputStreams
      6. Summary
    7. 6. Java Thread Scheduling
      1. An Overview of Thread Scheduling
        1. Scheduling Example: Threads of Different Priorities
        2. Scheduling Equal-Priority Threads
        3. Priority Inversion and Inheritance
        4. Round-Robin Scheduling
        5. Threading Models
      2. When Scheduling Is Important
        1. Round-Robin Scheduling and “Fairness”
      3. Scheduling with Thread Priorities
        1. Priority-Related Calls in the Java API
        2. Using the Priority Calls
        3. When to Use Simple Priority-Based Calls
      4. Popular Scheduling Implementations
        1. Green Threads
          1. Scheduling of green threads
        2. Windows Native Threads
          1. Scheduling of Windows native threads
        3. Solaris Native Threads
          1. Scheduling of Solaris native threads
          2. LWPs in the virtual machine
      5. Native Scheduling Support
        1. Implementing CPUSupport on Windows
        2. Implementing CPUSupport on Solaris
      6. Other Thread-Scheduling Methods
        1. The suspend() and resume() Methods
          1. Alternatives to the suspend() and resume() methods
        2. The yield() Method
          1. Yielding versus priority-based scheduling
        3. Daemon Threads
      7. Summary
    8. 7. Java Thread Scheduling Examples
      1. Thread Pools
      2. Round-Robin Scheduling
        1. A Simple Round-Robin Scheduler
        2. A More Complete Scheduler
          1. Adjustment 1: Synchronizing data within the CPUScheduler
          2. Adjustment 2: Making CPUScheduler thread safe
          3. Adjustment 3: More thread-safe modifications
          4. Adjustment 4: Devising an exit mechanism
          5. Adjustment 5: Non-CPU-intensive threads
      3. Job Scheduling
      4. Summary
    9. 8. Advanced Synchronization Topics
      1. Synchronization Terms
      2. Preventing Deadlock
        1. Another Type of Deadlock
      3. Lock Starvation
        1. Reader-Writer Locks
        2. Priority-Inverting Locks
      4. Thread-Unsafe Classes
        1. Explicit Synchronization
          1. Explicit synchronization and native code
        2. Single-Thread Access
          1. Using the Java Foundation Classes
          2. The event-dispatching thread and event-related method
          3. The invokeAndWait() method
          4. The invokeLater() method
          5. The repaint() method
          6. Other thread-unsafe classes
      5. Summary
    10. 9. Parallelizing for Multiprocessor Machines
      1. Parallelizing a Single-Threaded Program
        1. Loop Scheduling and Load Balancing
        2. Variable Classifications
          1. Loop-private variables
          2. Read-only variables
          3. Storeback variables
          4. Reduction variables
          5. Shared variables
        3. Loop Analysis and Transformations
          1. Loop distribution
          2. Loop isolation
          3. Loop interchange
          4. Loop reimplementation
      2. Inner-Loop Threading
      3. Loop Printing
      4. Multiprocessor Scaling
      5. Summary
    11. 10. Thread Groups
      1. Thread Group Concepts
      2. Creating Thread Groups
      3. Thread Group Methods
        1. Finding Thread Groups
        2. Enumerating Thread Groups
        3. Thread Group Priority Calls
        4. Destroying Thread Groups
        5. Daemon Thread Groups
        6. Miscellaneous Methods
      4. Manipulating Thread Groups
      5. Thread Groups, Threads, and Security
      6. Summary
    12. A. Miscellaneous Topics
      1. Thread Stack Information
      2. General Thread Information
      3. Default Exception Handler
      4. The ThreadDeath Class
        1. Inheriting from the ThreadDeath Class
        2. More on Thread Destruction
      5. The Volatile Keyword
    13. B. Exceptions and Errors
      1. InterruptedException
      2. InterruptedIOException
      3. NoSuchMethodError
      4. RuntimeException
        1. IllegalThreadStateException
        2. IllegalArgumentException
        3. IllegalMonitorStateException
        4. NullPointerException
        5. SecurityException
        6. Arbitrary Exceptions
    14. Index
    15. Colophon

Product information

  • Title: Java Threads, Second Edition
  • Author(s):
  • Release date: January 1999
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781565924185