You are previewing Concurrent Programming in Java™: Design Principles and Patterns, Second Edition.
O'Reilly logo
Concurrent Programming in Java™: Design Principles and Patterns, Second Edition

Book Description

In this second edition, you will find thoroughly updated coverage of the Javao 2 platform and new or expanded coverage of:

  • Memory model

  • Cancellation

  • Portable parallel programming

  • Utility classes for concurrency control

The Java platform provides a broad and powerful set of APIs, tools, and technologies. One of its most powerful capabilities is the built-in support for threads. This makes concurrent programming an attractive yet challenging option for programmers using the Java programming language.

This book shows readers how to use the Java platform's threading model more precisely by helping them to understand the patterns and tradeoffs associated with concurrent programming.

You will learn how to initiate, control, and coordinate concurrent activities using the class java.lang.Thread, the keywords synchronized and volatile, and the methods wait, notify, and notifyAll. In addition, you will find detailed coverage of all aspects of concurrent programming, including such topics as confinement and synchronization, deadlocks and conflicts, state-dependent action control, asynchronous message passing and control flow, coordinated interaction, and structuring web-based and computational services.

The book targets intermediate to advanced programmers interested in mastering the complexities of concurrent programming. Taking a design pattern approach, the book offers standard design techniques for creating and implementing components that solve common concurrent programming challenges. The numerous code examples throughout help clarify the subtleties of the concurrent programming concepts discussed.



0201310090B04062001

Table of Contents

  1. Copyright
  2. Acknowledgments
  3. 1. Concurrent Object-Oriented Programming
    1. 1.1. Using Concurrency Constructs
      1. 1.1.1. A Particle Applet
        1. 1.1.1.1. Particle
          1. Notes:
        2. 1.1.1.2. ParticleCanvas
        3. 1.1.1.3. ParticleApplet
          1. Notes:
      2. 1.1.2. Thread Mechanics
        1. 1.1.2.1. Construction
        2. 1.1.2.2. Starting threads
        3. 1.1.2.3. Priorities
        4. 1.1.2.4. Control methods
        5. 1.1.2.5. Static methods
        6. 1.1.2.6. ThreadGroups
      3. 1.1.3. Further Readings
    2. 1.2. Objects and Concurrency
      1. 1.2.1. Concurrency
      2. 1.2.2. Concurrent Execution Constructs
        1. 1.2.2.1. Computer systems
        2. 1.2.2.2. Processes
        3. 1.2.2.3. Threads
        4. 1.2.2.4. Tasks and lightweight executable frameworks
      3. 1.2.3. Concurrency and OO Programming
        1. 1.2.3.1. Sequential OO programming
        2. 1.2.3.2. Event-based programming
        3. 1.2.3.3. Concurrent systems programming
        4. 1.2.3.4. Other concurrent programming languages
      4. 1.2.4. Object Models and Mappings
        1. 1.2.4.1. Object models
        2. 1.2.4.2. Sequential mappings
        3. 1.2.4.3. Active objects
        4. 1.2.4.4. Mixed models
      5. 1.2.5. Further Readings
        1. 1.2.5.1. Concurrent programming
        2. 1.2.5.2. Models
        3. 1.2.5.3. Distributed systems
        4. 1.2.5.4. Real-time programming
    3. 1.3. Design Forces
      1. 1.3.1. Safety
        1. 1.3.1.1. Attributes and constraints
        2. 1.3.1.2. Representational constraints
      2. 1.3.2. Liveness
      3. 1.3.3. Performance
      4. 1.3.4. Reusability
        1. 1.3.4.1. Closed subsystems
        2. 1.3.4.2. Open systems
        3. 1.3.4.3. Documentation
      5. 1.3.5. Further Readings
    4. 1.4. Before/After Patterns
      1. 1.4.1. Layering
      2. 1.4.2. Adapters
      3. 1.4.3. Subclassing
        1. 1.4.3.1. Template methods
      4. 1.4.4. Method Adapters
      5. 1.4.5. Further Readings
  4. 2. Exclusion
    1. 2.1. Immutability
      1. 2.1.1. Applications
        1. 2.1.1.1. Abstract Data Types (ADTs)
        2. 2.1.1.2. Value containers
        3. 2.1.1.3. Sharing
      2. 2.1.2. Construction
    2. 2.2. Synchronization
      1. 2.2.1. Mechanics
        1. 2.2.1.1. Objects and locks
        2. 2.2.1.2. Synchronized methods and blocks
        3. 2.2.1.3. Acquiring and releasing locks
        4. 2.2.1.4. Statics
      2. 2.2.2. Fully Synchronized Objects
      3. 2.2.3. Traversal
        1. 2.2.3.1. Synchronized aggregate operations
        2. 2.2.3.2. Indexed traversal and client-side locking
        3. 2.2.3.3. Versioned iterators
        4. 2.2.3.4. Visitors
      4. 2.2.4. Statics and Singletons
      5. 2.2.5. Deadlock
      6. 2.2.6. Resource Ordering
      7. 2.2.7. The Java Memory Model
        1. 2.2.7.1. Atomicity
        2. 2.2.7.2. Visibility
        3. 2.2.7.3. Ordering
        4. 2.2.7.4. Volatile
      8. 2.2.8. Further Readings
    3. 2.3. Confinement
      1. 2.3.1. Confinement Across Methods
        1. 2.3.1.1. Sessions
        2. 2.3.1.2. Alternative protocols
      2. 2.3.2. Confinement Within Threads
        1. 2.3.2.1. Thread-specific fields
        2. 2.3.2.2. ThreadLocal
        3. 2.3.2.3. Applications and consequences
      3. 2.3.3. Confinement Within Objects
        1. 2.3.3.1. Adapters
        2. 2.3.3.2. Subclassing
      4. 2.3.4. Confinement Within Groups
        1. 2.3.4.1. Rings
      5. 2.3.5. Further Readings
    4. 2.4. Structuring and Refactoring Classes
      1. 2.4.1. Reducing Synchronization
        1. 2.4.1.1. Accessors
        2. 2.4.1.2. Double-check
        3. 2.4.1.3. Open calls
      2. 2.4.2. Splitting Synchronization
        1. 2.4.2.1. Splitting classes
        2. 2.4.2.2. Splitting locks
        3. 2.4.2.3. Isolating fields
        4. 2.4.2.4. Linked data structures
      3. 2.4.3. Read-Only Adapters
      4. 2.4.4. Copy-on-Write
        1. 2.4.4.1. Internal copy-on-write
        2. 2.4.4.2. Optimistic Updates
        3. 2.4.4.3 . Atomic commitment
      5. 2.4.5. Open Containers
        1. 2.4.5.1. Internal disciplines
        2. 2.4.5.2. External disciplines
        3. 2.4.5.3. Multilevel containment
      6. 2.4.6. Further Readings
    5. 2.5. Using Lock Utilities
      1. 2.5.1. Mutexes
        1. 2.5.1.1. Method adapters
        2. 2.5.1.2. Back-offs
        3. 2.5.1.3. Reorderings
        4. 2.5.1.4. Non-block-structured locking
        5. 2.5.1.5. Lock Ordering Managers
      2. 2.5.2. Read-Write Locks
      3. 2.5.3. Further Readings
  5. 3. State Dependence
    1. 3.1. Dealing with Failure
      1. 3.1.1. Exceptions
        1. 3.1.1.1. Abrupt termination
        2. 3.1.1.2. Continuation
        3. 3.1.1.3. Rollback
        4. 3.1.1.4. Roll-forward
        5. 3.1.1.5. Retry
        6. 3.1.1.6. Handlers
      2. 3.1.2. Cancellation
        1. 3.1.2.1. Interruption
        2. 3.1.2.2. IO and resource revocation
        3. 3.1.2.3. Asynchronous termination
        4. 3.1.2.4. Resource control
        5. 3.1.2.5. Multiphase cancellation
      3. 3.1.3. Further Readings
    2. 3.2. Guarded Methods
      1. 3.2.1. Guarded Suspension
        1. 3.2.1.1. Guards
        2. 3.2.1.2. State-based message acceptance
        3. 3.2.1.3. Defining logical control state
      2. 3.2.2. Monitor Mechanics
      3. 3.2.3. Guarded Waits
        1. 3.2.3.1. Interrupted waits
      4. 3.2.4. Notifications
        1. 3.2.4.1. Slipped conditions and missed signals
        2. 3.2.4.2. Single notifications
      5. 3.2.5. Timed Waits
      6. 3.2.6. Busy Waits
        1. 3.2.6.1. Efficiency
        2. 3.2.6.2. Scheduling
        3. 3.2.6.3. Triggering
        4. 3.2.6.4. Synchronizing actions
        5. 3.2.6.5. Implementations
    3. 3.3. Structuring and Refactoring Classes
      1. 3.3.1. Tracking State
        1. 3.3.1.1. Channels and bounded buffers
        2. 3.3.1.2. State variables
      2. 3.3.2. Conflict Sets
        1. 3.3.2.1. Implementation
        2. 3.3.2.2. Variants and extensions
      3. 3.3.3. Subclassing
        1. 3.3.3.1. Readers and Writers
        2. 3.3.3.2. Layering Guards
        3. 3.3.3.3. Inheritance anomalies
      4. 3.3.4. Confinement and Nested Monitors
      5. 3.3.5. Further Readings
    4. 3.4. Using Concurrency Control Utilities
      1. 3.4.1. Semaphores
        1. 3.4.1.1. Mutual exclusion locks
        2. 3.4.1.2. Resource pools
        3. 3.4.1.3. Bounded buffers
        4. 3.4.1.4. Synchronous channels
        5. 3.4.1.5. Fairness and scheduling
        6. 3.4.1.6. Priorities
      2. 3.4.2. Latches
        1. 3.4.2.1. Latching variables and predicates
      3. 3.4.3. Exchangers
      4. 3.4.4. Condition Variables
      5. 3.4.5. Further Readings
    5. 3.5. Joint Actions
      1. 3.5.1. General Solutions
        1. 3.5.1.1. Structure
        2. 3.5.1.2. Classes and methods
        3. 3.5.1.3. Liveness
        4. 3.5.1.4. Example
      2. 3.5.2. Decoupling Observers
      3. 3.5.3. Further Readings
    6. 3.6. Transactions
      1. 3.6.1. Transaction Protocols
      2. 3.6.2. Transaction Participants
        1. 3.6.2.1. Interfaces
        2. 3.6.2.2. Implementations
      3. 3.6.3. Creating Transactions
        1. 3.6.3.1. Example
      4. 3.6.4. Vetoable Changes
      5. 3.6.5. Further Readings
    7. 3.7. Implementing Utilities
      1. 3.7.1. Acquire-Release Protocols
      2. 3.7.2. Delegated Actions
        1. 3.7.2.1. Design steps
        2. 3.7.2.2. Bounded buffers
        3. 3.7.2.3. Collapsing classes
      3. 3.7.3. Specific Notifications
        1. 3.7.3.1. Design steps
        2. 3.7.3.2. FIFO semaphores
      4. 3.7.4. Further Readings
  6. 4. Creating Threads
    1. 4.1. Oneway Messages
      1. 4.1.1. Message Formats
      2. 4.1.2. Open Calls
      3. 4.1.3. Thread-Per-Message
        1. 4.1.3.1. Executors
      4. 4.1.4. Worker Threads
        1. 4.1.4.1. Design choices
        2. 4.1.4.2. Event queues
        3. 4.1.4.3. Timers
      5. 4.1.5. Polling and Event-Driven IO
        1. 4.1.5.1. Event-driven tasks
        2. 4.1.5.2. Triggering
      6. 4.1.6. Further Readings
    2. 4.2. Composing Oneway Messages
      1. 4.2.1. Composition
        1. 4.2.1.1. Representations
        2. 4.2.1.2. Stages
        3. 4.2.1.3. Scripting
      2. 4.2.2. Assembly Line
        1. 4.2.2.1. Representations
        2. 4.2.2.2. Interfaces
        3. 4.2.2.3. Adapters
        4. 4.2.2.4. Sinks
        5. 4.2.2.5. Connections
        6. 4.2.2.6. Linear stages
        7. 4.2.2.7. Combiners
        8. 4.2.2.8. Collectors
        9. 4.2.2.9. Dual output stages
        10. 4.2.2.10. Sources
        11. 4.2.2.11. Coordination
      3. 4.2.3. Further Readings
    3. 4.3. Services in Threads
      1. 4.3.1. Completion Callbacks
        1. 4.3.1.1. Interfaces
        2. 4.3.1.2. Implementations
        3. 4.3.1.3. Guarding callback methods
      2. 4.3.2. Joining Threads
      3. 4.3.3. Futures
        1. 4.3.3.1. Callables
      4. 4.3.4. Scheduling Services
      5. 4.3.5. Further Readings
    4. 4.4. Parallel Decomposition
      1. 4.4.1. Fork/Join
        1. 4.4.1.1. Task granularity and structure
        2. 4.4.1.2. Frameworks
        3. 4.4.1.3. Defining tasks
        4. 4.4.1.4. Fibonacci
          1. Notes:
        5. 4.4.1.5. Linking subtasks
        6. 4.4.1.6. Callbacks
          1. Notes:
        7. 4.4.1.7. Cancellation
      2. 4.4.2. Computation Trees
        1. 4.4.2.1. Building and using trees
      3. 4.4.3. Barriers
      4. 4.4.4. Further Readings
    5. 4.5. Active Objects
      1. 4.5.1. CSP
        1. 4.5.1.1. Processes and channels
        2. 4.5.1.2. Composition
        3. 4.5.1.3. JCSP
        4. 4.5.1.4. Dining philosophers
      2. 4.5.2. Further Readings