生產(chǎn)者
public class Producer {
private Depot depot;
public Producer(Depot depot) {
this.depot = depot;
}
public void produce(final int val) {
new Thread() {
public void run() {
depot.produce(val);
}
}.start();
}
}
消費(fèi)者
public class Consumer {
private Depot depot;
public Consumer(Depot depot) {
this.depot = depot;
}
public void consume(final int val) {
new Thread() {
public void run() {
depot.consume(val);
}
}.start();
}
}
倉(cāng)庫(kù)類
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Depot {
private int capacity;//倉(cāng)庫(kù)容量
private int size;//倉(cāng)庫(kù)當(dāng)前存量
private Lock lock;//獨(dú)占鎖
private Condition fullCondition;//生產(chǎn)條件
private Condition emptyCondition;//消費(fèi)條件
public Depot() {
this.capacity = 100;
this.size = 0;
this.lock = new ReentrantLock();
this.fullCondition = lock.newCondition();
this.emptyCondition = lock.newCondition();
}
public Depot(int capacity) {
this.capacity = capacity;
this.size = 0;
this.lock = new ReentrantLock();
this.fullCondition = lock.newCondition();
this.emptyCondition = lock.newCondition();
}
public void produce(int val) {
lock.lock();
try{
int left = val;
while(left > 0) {
//庫(kù)存已滿,等待生產(chǎn)
while(size >= capacity) {
fullCondition.await();
}
//倉(cāng)庫(kù)當(dāng)前容量+要生產(chǎn)的數(shù)量>倉(cāng)庫(kù)容量,則需要生產(chǎn)的數(shù)量為倉(cāng)庫(kù)容量-當(dāng)前倉(cāng)庫(kù)的庫(kù)存量
//否則荒典,需要生產(chǎn)的數(shù)量為傳入的生產(chǎn)量
int inc = (size + left) > capacity? capacity - size : left;//實(shí)際生產(chǎn)的數(shù)量
size += inc;//當(dāng)前庫(kù)存
left -= inc;//還需要生產(chǎn)的數(shù)量
System.out.printf("%s produce(%3d) --> left=%3d, inc=%3d, size=%3d\n",
Thread.currentThread().getName(), val, left, inc, size);
emptyCondition.signal();//通知消費(fèi)
}
// emptyCondition.signal();//放在此處當(dāng)size=capacity時(shí)驶兜,生產(chǎn)線程一直等待,且無(wú)法喚醒消費(fèi)線程
}catch(InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void consume(int val) {
lock.lock();
try{
int left = val;
while(left > 0) {
//當(dāng)沒(méi)有庫(kù)存量時(shí)俊扳,等待消費(fèi)
while(size <= 0) {
emptyCondition.await();
}
//庫(kù)存量大于需要消費(fèi)的數(shù)量時(shí)途蒋,則消費(fèi)需要消費(fèi)的數(shù)量
//否則,消費(fèi)當(dāng)前的庫(kù)存量
int dec = size > left? left : size;//消費(fèi)的數(shù)量
size -= dec;//消費(fèi)后的庫(kù)存
left -= dec;//還需消費(fèi)的數(shù)量
System.out.printf("%s consume(%3d) <-- left=%3d, dec=%3d, size=%3d\n",
Thread.currentThread().getName(), val, left, dec, size);
fullCondition.signal();//通知生產(chǎn)
}
// fullCondition.signal();//放在此處當(dāng)size=0時(shí)馋记,消費(fèi)一直等待号坡,且無(wú)法喚醒生產(chǎn)線程
}catch(InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
測(cè)試代碼
public static void main(String[] args) {
Depot depot = new Depot(100);
Producer producer = new Producer(depot);
Consumer consumer = new Consumer(depot);
producer.produce(50);
consumer.consume(70);
producer.produce(120);
consumer.consume(30);
}
打印結(jié)果
Thread-0 produce( 50) --> left= 0, inc= 50, size= 50
Thread-1 consume( 70) <-- left= 20, dec= 50, size= 0
Thread-2 produce(120) --> left= 20, inc=100, size=100
Thread-1 consume( 70) <-- left= 0, dec= 20, size= 80
Thread-2 produce(120) --> left= 0, inc= 20, size=100
Thread-3 consume( 30) <-- left= 0, dec= 30, size= 70