You are previewing Java 7 Concurrency Cookbook.
O'Reilly logo
Java 7 Concurrency Cookbook

Book Description

Master multithreaded program development with this Java 7 concurrency cookbook. Packed with real-world solutions, it takes you from basic to more sophisticated aspects of concurrency. For intermediate to advanced Java developers.

  • Master all that Java 7 has to offer for concurrent programming

  • Get to grips with thread management, the Fork/Join framework, concurrency classes and much more in this book and e-book

  • A practical Cookbook packed with recipes for achieving the most important Java Concurrency tasks

  • In Detail

    Java remains the global standard for developing various applications and enterprise software, and the launch of Java 7 brings with it exciting new capabilities for concurrent programming by way of the concurrency utilities enhancement. This allows developers to make the most of their applications with parallel task performance. "Java 7 Concurrency Cookbook" covers all elements of the Java concurrency API, providing essential recipes for taking advantage of the exciting new capabilities.

    On your computer, you can listen to music while you edit a Word document and read your emails, all at once! This is because your operating system allows the concurrency of tasks, much like the Java platform which offers various classes to execute concurrent tasks inside a Java program. "Java 7 Concurrency Cookbook" covers the most important features of the Java concurrency API, with special emphasis on the new capabilities of version 7.

    With each version, Java increases the available functionality to facilitate development of concurrent programs. This book covers the most important and useful mechanisms included in version 7 of the Java concurrency API, so you will be able to use them directly in your applications.

    "Java 7 Concurrency Cookbook" includes recipes to enable you to achieve everything from the basic management of threads and tasks, to the new Fork /Join framework, through synchronization mechanisms between tasks, different types of concurrent tasks that Java can execute, data structures that must be used in concurrent applications and the classes of the library that can be customized.

    With the step-by-step examples in this book you’ll be able to apply the most important and useful features of the Java 7 concurrency API.

    Table of Contents

    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
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      7. 1. Thread Management
        1. Introduction
        2. Creating and running a thread
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Getting and setting thread information
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See Also
        4. Interrupting a thread
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Controlling the interruption of a thread
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Sleeping and resuming a thread
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Waiting for the finalization of a thread
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Creating and running a daemon thread
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Processing uncontrolled exceptions in a thread
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        10. Using local thread variables
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        11. Grouping threads into a group
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        12. Processing uncontrolled exceptions in a group of threads
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        13. Creating threads through a factory
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      8. 2. Basic Thread Synchronization
        1. Introduction
        2. Synchronizing a method
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Arranging independent attributes in synchronized classes
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Using conditions in synchronized code
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Synchronizing a block of code with a Lock
          1. Getting Ready...
          2. How to do it...
          3. How it works...
          4. There's more...
            1. More Info
          5. See also
        6. Synchronizing data access with read/write locks
          1. Getting Ready...
          2. How to do it...
          3. How it works...
          4. See also
        7. Modifying Lock fairness
          1. Getting Ready...
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        8. Using multiple conditions in a Lock
          1. Getting Ready...
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      9. 3. Thread Synchronization Utilities
        1. Introduction
        2. Controlling concurrent access to a resource
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Fairness in semaphores
          5. See also
        3. Controlling concurrent access to multiple copies of a resource
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Waiting for multiple concurrent events
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Synchronizing tasks in a common point
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Resetting a CyclicBarrier object
            2. Broken CyclicBarrier objects
          5. See also
        6. Running concurrent phased tasks
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Registering participants in the Phaser
            2. Forcing the termination of a Phaser
          5. See also
        7. Controlling phase change in concurrent phased tasks
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        8. Changing data between concurrent tasks
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      10. 4. Thread Executors
        1. Introduction
        2. Creating a thread executor
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Creating a fixed-size thread executor
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Executing tasks in an executor that returns a result
          1. Getting ready...
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Running multiple tasks and processing the first result
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Running multiple tasks and processing all the results
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Running a task in an executor after a delay
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        8. Running a task in an executor periodically
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        9. Canceling a task in an executor
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        10. Controlling a task finishing in an executor
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        11. Separating the launching of tasks and the processing of their results in an executor
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        12. Controlling rejected tasks of an executor
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      11. 5. Fork/Join Framework
        1. Introduction
        2. Creating a Fork/Join pool
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Joining the results of the tasks
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        4. Running tasks asynchronously
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        5. Throwing exceptions in the tasks
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Canceling a task
          1. Getting ready...
          2. How to do it...
          3. How it works...
          4. See also
      12. 6. Concurrent Collections
        1. Introduction
        2. Using non-blocking thread-safe lists
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Using blocking thread-safe lists
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Using blocking thread-safe lists ordered by priority
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Using thread-safe lists with delayed elements
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Using thread-safe navigable maps
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Generating concurrent random numbers
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        8. Using atomic variables
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        9. Using atomic arrays
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      13. 7. Customizing Concurrency Classes
        1. Introduction
        2. Customizing the ThreadPoolExecutor class
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        3. Implementing a priority-based Executor class
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Implementing the ThreadFactory interface to generate custom threads
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Using our ThreadFactory in an Executor object
          1. Getting ready...
          2. How to do it...
          3. How it works...
          4. See also
        6. Customizing tasks running in a scheduled thread pool
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Implementing the ThreadFactory interface to generate custom threads for the Fork/Join framework
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        8. Customizing tasks running in the Fork/Join framework
          1. How to do it...
          2. How it works...
          3. See also
        9. Implementing a custom Lock class
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        10. Implementing a transfer Queue based on priorities
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        11. Implementing your own atomic object
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      14. 8. Testing Concurrent Applications
        1. Introduction
        2. Monitoring a Lock interface
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Monitoring a Phaser class
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        4. Monitoring an Executor framework
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Monitoring a Fork/Join pool
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Writing effective log messages
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Analyzing concurrent code with FindBugs
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        8. Configuring Eclipse for debugging concurrency code
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Configuring NetBeans for debugging concurrency code
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        10. Testing concurrency code with MultithreadedTC
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also