Import Java.util.concurrent.locks.Lock;
Import Java.util.concurrent.locks.ReentrantLock;
Import java.util.concurrent.locks.Condition; Warehouse class Depot {private int capacity; The capacity of the warehouse private int size; Inventory quantity of the warehouse private lock lock; Exclusive lock private Condition fullcondtion; Production conditions private Condition emptycondtion;
Consumption conditions public Depot (int capacity) {this.capacity = capacity;
this.size = 0;
This.lock = new Reentrantlock ();
This.fullcondtion = Lock.newcondition ();
This.emptycondtion = Lock.newcondition ();
public void produce (int val) {lock.lock ();
try {//left indicates the quantity you want to produce (it is possible to produce too much, multiple production) int left = Val;
while (Left > 0) {//Inventory is full, wait for "consumer" consumer product.
while (size >= capacity) fullcondtion.await (); Get "Quantity actually produced" (that is, new quantity in Inventory)//if "stock" + "Quantity to be produced";“ Total capacity ", then" actual increment "=" Total Capacity "-" current capacity ". (At this point fill the warehouse)//otherwise "actual increment" = "Quantity wanted to produce" int inc = (size+left) >capacity?
(capacity-size): left;
Size + + Inc;
Left-= Inc; System.out.printf ("%s produce (%3d)--> left=%3d, inc=%3d, size=%3d\n", Thread.CurrentThread ().
GetName (), Val, left, Inc, size);
Notice that "consumers" can be consumed.
Emptycondtion.signal ();
The catch (Interruptedexception e) {} finally {Lock.unlock ();
} public void consume (int val) {lock.lock ();
try {//left indicates "customer wants to consume quantity" (it is possible that consumption is too large, inventory is insufficient, multiple consumption is required) int left = Val;
while (Left > 0) {//Inventory is 0 o'clock, wait for "producer" to produce the product.
while (size <= 0) emptycondtion.await (); Obtain "Quantity of actual consumption" (i.e. quantity actually reduced in inventory)//"Actual consumption" = "Inventory" if "inventory" < "number of customers to consume";//otherwise, "actual Consumption" = "The quantity that the customer wants to consume". int dec = (size<left)?
Size:left;
size = Dec;
Left-= Dec; System.out.printf ("%s consume (%3d) <--left=%3d, dec=%3d, size=%3d\n", Thread.CurrentThread ().
GetName (), Val, left, Dec, size);
Fullcondtion.signal ();
The catch (Interruptedexception e) {} finally {Lock.unlock ();
} public String toString () {return "Capacity:" +capacity+ ", Actual size:" +size;
}
};
Producer class Producer {private Depot Depot;
Public Producer (Depot Depot) {this.depot = Depot;
//Consumer Products: Create a new thread to produce the product in the warehouse. public void produce (final int val) {new Thread () {public void run () {Depot.produce (
Val);
}}.start ();
}//Consumer class Customer {private Depot Depot; Public Customer (Depot Depot) {This.depoT = Depot;
//Consumer Products: Create a new thread to consume the product from the warehouse. public void consume (final int val) {new Thread () {public void run () {Depot.consume (
Val);
}}.start ();
} public class LockTest3 {public static void main (string[] args) {Depot mdepot = new Depot (100);
Producer Mpro = new Producer (mdepot);
Customer MCUs = new Customer (mdepot);
Mpro.produce (60);
Mpro.produce (120);
Mcus.consume (90);
Mcus.consume (150);
Mpro.produce (110); }
}