java生产者消费者_Java——Java实现生产者消费者

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\n",

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

size -= dec;

left -= dec;

System.out.printf("%s consume(%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)

Thread-3 consume(150)

Thread-1 produce(120) --> left= 20, inc=100, size=100

Thread-3 consume(150)

Thread-4 produce(110) --> left= 0, inc= 70, size= 70

Thread-3 consume(150)

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\n",

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)

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)

Thread-3 consume(150)

Thread-4 produce(110) --> left= 10, inc=100, size=100

Thread-3 consume(150)

Thread-4 produce(110) --> left= 0, inc= 10, size= 10

Thread-3 consume(150)

Thread-1 produce(120) --> left= 0, inc= 80, size= 80

Thread-3 consume(150)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值