1、生产/消费者模型
生产/消费者问题是个非常典型的多线程问题,涉及到的对象包括“生产者”、“消费者”、“仓库”和“产品”。他们之间的关系如下:
(01) 生产者仅仅在仓储未满时候生产,仓满则停止生产。
(02) 消费者仅仅在仓储有产品时候才能消费,仓空则等待。
(03) 当消费者发现仓储没产品可消费时候会通知生产者生产。
(04) 生产者在生产出可消费产品时候,应该通知等待的消费者去消费。
2、生产者消费者实现(synchronized )
// Demo1.java
// 仓库
class Depot {
private int capacity; // 仓库的容量
private int size; // 仓库的实际数量
public Depot(int capacity) {
this.capacity = capacity;
this.size = 0;
}
public synchronized void produce(int val) {
try {
// left 表示“想要生产的数量”(有可能生产量太多,需多此生产)
int left = val;
while (left > 0) {
// 库存已满时,等待“消费者”消费产品。
while (size >= capacity)
wait();
// 获取“实际生产的数量”(即库存中新增的数量)
// 如果“库存”+“想要生产的数量”>“总的容量”,则“实际增量”=“总的容量”-“当前容量”。(此时填满仓库)
// 否则“实际增量”=“想要生产的数量”
int inc = (size+left)>capacity ? (capacity-size) : left;
size += inc;
left -= inc;
System.out.printf("%s produce(%3d) --> left=%3d, inc=%3d, size=%3d
",
Thread.currentThread().getName(), val, left, inc, size);
// 通知“消费者”可以消费了。
notifyAll();
}
} catch (InterruptedException e) {
}
}
public synchronized void consume(int val) {
try {
// left 表示“客户要消费数量”(有可能消费量太大,库存不够,需多此消费)
int left = val;
while (left > 0) {
// 库存为0时,等待“生产者”生产产品。
while (size <= 0)
wait();
// 获取“实际消费的数量”(即库存中实际减少的数量)
// 如果“库存”<“客户要消费的数量”,则“实际消费量”=“库存”;
// 否则,“实际消费量”=“客户要消费的数量”。
int dec = (size<left) ? size : left;
size -= dec;
left -= dec;
System.out.printf("%s consume(%3d) <-- left=%3d, dec=%3d, size=%3d
",
Thread.currentThread().getName(), val, left, dec, size);
notifyAll();
}
} catch (InterruptedException e) {
}
}
public String toString() {
return "capacity:"+capacity+", actual size:"+size;
}
}
// 生产者
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();
}
}
// 消费者
class Customer {
private Depot depot;
public Customer(Depot depot) {
this.depot = depot;
}
// 消费产品:新建一个线程从仓库中消费产品。
public void consume(final int val) {
new Thread() {
public void run() {
depot.consume(val);
}
}.start();
}
}
public class Demo1 {
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);
}
}
运行结果
Thread-0 produce( 60) --> left= 0, inc= 60, size= 60
Thread-4 produce(110) --> left= 70, inc= 40, size=100
Thread-2 consume( 90) <-- left= 0, dec= 90, size= 10
Thread-3 consume(150) <-- left=140, dec= 10, size= 0
Thread-1 produce(120) --> left= 20, inc=100, size=100
Thread-3 consume(150) <-- left= 40, dec=100, size= 0
Thread-4 produce(110) --> left= 0, inc= 70, size= 70
Thread-3 consume(150) <-- left= 0, dec= 40, size= 30
Thread-1 produce(120) --> left= 0, inc= 20, size= 50
3、生产者消费者实现(Lock锁)
package Thread;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Depot{
private int capacity; //仓库容量
private int size; //仓库当前产品数量
private Lock lock; //锁
private Condition productCondition; //生产条件
private Condition consumerCondition; //消费条件
public Depot(int capacity) {
this.capacity=capacity;
this.size=0;
this.lock = new ReentrantLock();
this.productCondition = this.lock.newCondition();
this.consumerCondition = this.lock.newCondition();
}
//生产商品
public void produce(int val) {
lock.lock();
try {
int left=val; //left表示需要生产的数量
while (left > 0) {
//当库存已满时,则生产者开始等待消费者消费
while (size == capacity) {
productCondition.await();
}
int pro= left > (capacity-size) ? (capacity-size) : left; //如果生产的数量大于仓库容量,则只生产仓库容量这么多
size += pro;
left -= pro;
System.out.printf("%s produce(%3d) --> left=%3d, inc=%3d, size=%3d
",
Thread.currentThread().getName(), val, left, pro, size);
consumerCondition.signal(); //消费者消费时,发现当仓库产品为零时,此时消费者等待,调用生产者生产,那么生产者生产完成后则需要释放消费者
}
}catch (Exception e){
}finally {
lock.unlock();
}
}
//消费商品
public void consume(int val) {
lock.lock();
try {
int left=val; //left表示需要消费的数量
while (left > 0) {
while (size == 0) { //当前商品数量为零时,消费者需要等待
consumerCondition.await();
}
int cus=left > size? size : left;
size -= cus;
left -= cus;
System.out.printf("%s consume(%3d) <-- left=%3d, dec=%3d, size=%3d
",
Thread.currentThread().getName(), val, left, cus, size);
productCondition.signal(); //消费者消费完成后,需要释放生产者
}
} catch (Exception e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
//生产者
class Producer{
private Depot depot;
public Producer(Depot depot) {
this.depot = depot;
}
public void produce(int val){
new Thread(){
@Override
public void run() {
depot.produce(val);
}
}.start();
}
}
//消费者
class Customer{
private Depot depot;
public Customer(Depot depot) {
this.depot = depot;
}
public void consume(int val) {
new Thread(){
@Override
public void run() {
depot.consume(val);
}
}.start();
}
}
public class LockTest {
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);
}
}
运行结果
Thread-0 produce( 60) --> left= 0, inc= 60, size= 60
Thread-1 produce(120) --> left= 80, inc= 40, size=100
Thread-2 consume( 90) <-- left= 0, dec= 90, size= 10
Thread-3 consume(150) <-- left=140, dec= 10, size= 0
Thread-4 produce(110) --> left= 10, inc=100, size=100
Thread-3 consume(150) <-- left= 40, dec=100, size= 0
Thread-4 produce(110) --> left= 0, inc= 10, size= 10
Thread-3 consume(150) <-- left= 30, dec= 10, size= 0
Thread-1 produce(120) --> left= 0, inc= 80, size= 80
Thread-3 consume(150) <-- left= 0, dec= 30, size= 50