您的位置:首页 > 其它

生产者与消费者问题

2016-03-18 22:54 337 查看
听说面试会考到生产者消费者问题,今天看了下,根据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);

}

}

}

}

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

只是实现了最简单的生产者和消费者问题~哇哈哈,欢迎交流
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: