生产者消费者模式的三种实现方式

原作者:老铁123   
出处:https://blog.csdn.net/qewgd/article/details/85926275   
本文归作者【老铁123】和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

生产者消费者模式
1、生产者只在仓库未满时进行生产,仓库满时生产者进程被阻塞;
2、消费者只在仓库非空时进行消费,仓库为空时消费者进程被阻塞;

一、synchronized方式实现
代码如下

**ProduceAndConsumeBySynchronized.java**
public class ProduceAndConsumeBySynchronized{
	// TODO多线程调用
}

class StoreWN {
	public int capacity = 10;
	public int index = 0;

	public synchronized void produce() {
		while (index >= capacity) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		index++;
		notifyAll();
	}

	public synchronized void consume() {
		while (index <= 0) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		index--;
		notifyAll();
	}
}

class Producer implements Runnable {
	StoreWN swn = null;

	public Producer(StoreWN swn) {
		this.swn = swn;
	}

	public void run() {
		swn.produce();
	}
}

class Consumer implements Runnable {
	StoreWN swn = null;

	public Consumer(StoreWN swn) {
		this.swn = swn;
	}

	public void run() {
		swn.consume();
	}
}

二、Lock API方式实现
代码如下

**ProduceAndConsumeByLockApi.java**

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ProduceAndConsumeByLockApi {
	// TODO多线程调用
}

class StoreAS {
	public int capacity = 10;
	public int index = 0;
	Lock lock = new ReentrantLock();
	Condition full = lock.newCondition();
	Condition empty = lock.newCondition();

	public void produce() {
		lock.lock();
		try {
			while (index >= capacity) {
				try {
					full.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			index++;
			empty.signalAll();
		} finally {
			lock.unlock();
		}
	}

	public void consume() {
		lock.lock();
		try {
			while (index <= capacity) {
				try {
					empty.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			index--;
			full.signalAll();
		} finally {
			lock.unlock();
		}
	}
}

class ProducerAS implements Runnable {
	StoreAS swn = null;

	public ProducerAS(StoreAS swn) {
		this.swn = swn;
	}

	public void run() {
		swn.produce();
	}
}

class ConsumerAS implements Runnable {
	StoreAS swn = null;

	public ConsumerAS(StoreAS swn) {
		this.swn = swn;
	}

	public void run() {
		swn.consume();
	}
}

三、阻塞队列方式实现(底层就是实现方式二)
代码如下

**ProduceAndConsumeByBlockingQueue.java**

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class ProduceAndConsumeByBlockingQueue {
	// TODO多线程调用
}

class StoreBQ {
	BlockingQueue<Integer> bq = new ArrayBlockingQueue<Integer>(10);
	
	public void produce(){
		try {
			bq.put(1);
		} catch (InterruptedException e) {
		}
	}
	
	public void consume(){
		try {
			bq.take();
		} catch (InterruptedException e) {
		}
	}
}

class ProducerBQ implements Runnable {
	StoreBQ swn = null;

	public ProducerBQ(StoreBQ swn) {
		this.swn = swn;
	}

	public void run() {
		swn.produce();
	}
}

class ConsumerBQ implements Runnable {
	StoreBQ swn = null;

	public ConsumerBQ(StoreBQ swn) {
		this.swn = swn;
	}

	public void run() {
		swn.consume();
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值