One.
A lock hangs multiple sets of monitors, describes the Java.util.concurrent.locks package inside the lock interface and some features of the Conditon interface.
It is important to be clear what the lock interface and condition interface do.
Some people say that even an object is not, always say the interface, their own implementation? Lock and condition are exposed as interfaces, and we are using this interface, which is not implemented. Will it come true? It's impossible. Give us the interface, we can use, how to use? How it is implemented, how it is used. It's doing it by itself, and I'll just take it and get it done. The interface is extensible.
The API not only introduces the concept, but also gives examples to help us learn to use the corresponding concepts.
Here's a look at examples of condition (variables are used in the example, not the concept of producer and consumer),
Bounderbuffer is a buffer, which has a lock, two monitors, not full, non-empty. Previously defined is the name variable, which records the names of the roast duck. Here is the definition of an array container, which is the object, object[], white people are really defined to be able to pack 100 ducks basket. Putptr is stored, takeptr is taken, count is counted, and these are all variables used to manipulate the array.
This buffer has a total of two methods, one is stored, and one is taken.
What about this? You give me a duck (object x), and you get the lock when you come in Lock.lock (); If you get the lock, in the inside of the operation process, this buddy can take it? No, this lock is mutually exclusive. You get it, I can't get it. Now, when we get the lock, we come in and start judging, while judging. When judging the mark, be sure to use while. Why use while, because it's safe. Every time you wake up, you have to judge the mark, which is the safest thing to do, because if the mark is changed, nobody knows. Therefore, every time I wake up, I have a look, in the end there is no, I do not do, no I do. Signal is not all is not necessarily, if can realize to wake up each other, I do not need all, because all will bring the party wake. If you have more than one set of monitors hanging, you can wake them up. It's a little different to wake up the other person all the time. All Wake up, all live, but only one in the run, after running the mark, then another thread come over, judge the mark or hang, this thread simply does not need to come over. What about that? It's a matter of efficiency to wake up one. Count==items.length, the counter is determined by the length of the array, if equal, then baked in place, do not bake a direct await. Not fully baked? Come one, save one, items[putptr]=x. Putptr is a pointer.
java-Preliminary Understanding-14th chapter-Inter-threading communication-multi-producer multi-consumer problem-jdk1.5 new feature solution-Example