You are previewing Java 7 Concurrency Cookbook.

Java 7 Concurrency Cookbook

Cover of Java 7 Concurrency Cookbook by Javier Fernández González Published by Packt Publishing
  1. Java 7 Concurrency Cookbook
    1. Java 7 Concurrency Cookbook
    2. Credits
    3. About the Author
    4. About the Reviewers
    5. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
      2. Why Subscribe?
      3. Free Access for Packt account holders
      4. Instant Updates on New Packt Books
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
    7. 1. Thread Management
      1. Introduction
      2. Creating and running a thread
      3. Getting and setting thread information
      4. Interrupting a thread
      5. Controlling the interruption of a thread
      6. Sleeping and resuming a thread
      7. Waiting for the finalization of a thread
      8. Creating and running a daemon thread
      9. Processing uncontrolled exceptions in a thread
      10. Using local thread variables
      11. Grouping threads into a group
      12. Processing uncontrolled exceptions in a group of threads
      13. Creating threads through a factory
    8. 2. Basic Thread Synchronization
      1. Introduction
      2. Synchronizing a method
      3. Arranging independent attributes in synchronized classes
      4. Using conditions in synchronized code
      5. Synchronizing a block of code with a Lock
      6. Synchronizing data access with read/write locks
      7. Modifying Lock fairness
      8. Using multiple conditions in a Lock
    9. 3. Thread Synchronization Utilities
      1. Introduction
      2. Controlling concurrent access to a resource
      3. Controlling concurrent access to multiple copies of a resource
      4. Waiting for multiple concurrent events
      5. Synchronizing tasks in a common point
      6. Running concurrent phased tasks
      7. Controlling phase change in concurrent phased tasks
      8. Changing data between concurrent tasks
    10. 4. Thread Executors
      1. Introduction
      2. Creating a thread executor
      3. Creating a fixed-size thread executor
      4. Executing tasks in an executor that returns a result
      5. Running multiple tasks and processing the first result
      6. Running multiple tasks and processing all the results
      7. Running a task in an executor after a delay
      8. Running a task in an executor periodically
      9. Canceling a task in an executor
      10. Controlling a task finishing in an executor
      11. Separating the launching of tasks and the processing of their results in an executor
      12. Controlling rejected tasks of an executor
    11. 5. Fork/Join Framework
      1. Introduction
      2. Creating a Fork/Join pool
      3. Joining the results of the tasks
      4. Running tasks asynchronously
      5. Throwing exceptions in the tasks
      6. Canceling a task
    12. 6. Concurrent Collections
      1. Introduction
      2. Using non-blocking thread-safe lists
      3. Using blocking thread-safe lists
      4. Using blocking thread-safe lists ordered by priority
      5. Using thread-safe lists with delayed elements
      6. Using thread-safe navigable maps
      7. Generating concurrent random numbers
      8. Using atomic variables
      9. Using atomic arrays
    13. 7. Customizing Concurrency Classes
      1. Introduction
      2. Customizing the ThreadPoolExecutor class
      3. Implementing a priority-based Executor class
      4. Implementing the ThreadFactory interface to generate custom threads
      5. Using our ThreadFactory in an Executor object
      6. Customizing tasks running in a scheduled thread pool
      7. Implementing the ThreadFactory interface to generate custom threads for the Fork/Join framework
      8. Customizing tasks running in the Fork/Join framework
      9. Implementing a custom Lock class
      10. Implementing a transfer Queue based on priorities
      11. Implementing your own atomic object
    14. 8. Testing Concurrent Applications
      1. Introduction
      2. Monitoring a Lock interface
      3. Monitoring a Phaser class
      4. Monitoring an Executor framework
      5. Monitoring a Fork/Join pool
      6. Writing effective log messages
      7. Analyzing concurrent code with FindBugs
      8. Configuring Eclipse for debugging concurrency code
      9. Configuring NetBeans for debugging concurrency code
      10. Testing concurrency code with MultithreadedTC
O'Reilly logo

Synchronizing data access with read/write locks

One of the most significant improvements offered by locks is the ReadWriteLock interface and the ReentrantReadWriteLock class, the unique one that implements it. This class has two locks, one for read operations and one for write operations. There can be more than one thread using read operations simultaneously, but only one thread can be using write operations. When a thread is doing a write operation, there can't be any thread doing read operations.

In this recipe, you will learn how to use a ReadWriteLock interface implementing a program that uses it to control the access to an object that stores the prices of two products.

Getting Ready...

You should read the Synchronizing a block of code with a ...

The best content for your career. Discover unlimited learning on demand for around $1/day.