您的位置:首页 > 其它

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

2019-01-06 14:06 85 查看

原作者:老铁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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: