生产者与消费者问题

听说面试会考到生产者消费者问题,今天看了下,根据java的并发程序写了以下代码。


写代码最重要的是要弄清楚,一个类里面到底需要哪些属性以及方法,将这些搞清楚后就会发现代码写起来很流畅。


一. 单缓冲区 的生产者消费者问题

要有:1.仓库 2.消费者 3. 生产者

仓库:由于是单缓冲区,只需要一个boolean类型来判断仓库是否为空即可,以及货物的大小,以及放入和提取方法。

import javax.swing.text.StyledEditorKit.ForegroundAction;

public class CubbyHole {
private int goods;
private boolean empty;
public CubbyHole() {
empty=true;  //仓库一开始为空
}
public synchronized int get(int id){
while (empty) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("消费者拿走了货物"+goods);

empty=true;

notify();
return goods;
}
public synchronized void put(int val){
while (!empty) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
goods=val;
System.out.println("生产者"放入了货物"+goods;

empty=false;

notify();

}
}


主程序,图方便生产者和消费者作为内部类写了

public class Main {
public static void main(String[] args){
CubbyHole cubbyHole=new CubbyHole();
Producer producer=new Producer();
producer.start();
Consumer consumer=new Consumer();
consumers.start();

    }

public static class Producer extends Thread{
private CubbyHole cubbyHole;
public Producer(CubbyHole c) {
this.cubbyHole=c;
}
public void run(){
for(int i=0;i<50;i++){
cubbyHole.put((int)(100*Math.random()));
}
}
}
public static class Consumer extends Thread{
private CubbyHole cubbyHole;
public Consumer(CubbyHole c) {
this.cubbyHole=c;
}
public void run(){
for(int i=0;i<50;i++){
cubbyHole.get();
}
}
}

二.多缓冲区 的生产者消费者问题

要有:1.仓库 2.消费者 3. 生产者

仓库:由于是多缓冲区,需要一个参数nbuf设置缓冲大小,count记录仓库中的货物数量,head记录生产者每次存放货物的位置

rear记录消费者每次提取货物的位置,一个goods[]数组用来存放货物以及放入和提取方法。

import javax.swing.text.StyledEditorKit.ForegroundAction;


public class CubbyHole {
private int[] goods;
private int nbuf;
private int count;
private int front;
private int rear;
public CubbyHole(int nbuf) {
this.nbuf=nbuf;
goods=new int[nbuf];
count=0;
front=0;
rear=0;
}
public synchronized int get(int id){
while (count<=0) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
front=(front+1)%nbuf;
count--;
notifyAll();
System.out.println("消费者"+id+"拿走了货物"+goods[front]);
return goods[front];
}
public synchronized void put(int val,int id){
while (count>=nbuf) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
rear=(rear+1)%nbuf;
goods[rear]=val;
count++;
notifyAll();
System.out.println("生产者"+id+"放入了货物"+goods[rear]);
}
}

下面是主程序,定义了4个生成者和4个消费者一起执行 (不然不就没意义了,和单缓冲区的一样了)

public class Main {
public static void main(String[] args){
int nbuf=8;
CubbyHole cubbyHole=new CubbyHole(nbuf);
Producer[] producers=new Producer[4];
for(int i=0;i<4;i++){
producers[i]=new Producer(cubbyHole,i+1);
producers[i].start();
}
Consumer[] consumers=new Consumer[4];
for(int i=0;i<4;i++){
consumers[i]=new Consumer(cubbyHole,i+1);
consumers[i].start();
}

    }
public static class Producer extends Thread{
private CubbyHole cubbyHole;
private int id;
public Producer(CubbyHole c,int id) {
this.cubbyHole=c;
this.id=id;
}
public void run(){
for(int i=0;i<50;i++){
cubbyHole.put((int)(100*Math.random()),id);
}
}
}
public static class Consumer extends Thread{
private CubbyHole cubbyHole;
private int id;
public Consumer(CubbyHole c,int id) {
this.cubbyHole=c;
this.id=id;
}
public void run(){
for(int i=0;i<50;i++){
cubbyHole.get(id);
}
}
}
}

以上代码挺简单的,就不加什么详细的备注了。以下是多缓冲区生产者消费者问题的运行结果


只是实现了最简单的生产者和消费者问题~哇哈哈,欢迎交流




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值