-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Java 9 Concurrency Cookbook, Second Edition
By :

A lock may be associated with one or more conditions. These conditions are declared in the Condition
interface. The purpose of these conditions is to allow threads to have control of a lock and check whether a condition is true
or not. If it's false
, the thread will be suspended until another thread wakes it up. The Condition
interface provides the mechanisms to suspend a thread and wake up a suspended thread.
A classic problem in concurrent programming is the producer-consumer problem. We have a data buffer, one or more producers of data that save it in the buffer, and one or more consumers of data that take it from the buffer, as explained earlier in this chapter.
In this recipe, you will learn how to implement the producer-consumer problem using locks and conditions.
You should read the Synchronizing a block of code with a lock recipe to better understand this recipe.
Follow these steps to implement the example: