您的位置:首页 > 其它

马士兵-多线程学习第05课 生产者消费者问题

2016-11-01 21:58 447 查看
本人地址:http://blog.csdn.net/hblfyla/article/details/53001523

要求实现如下如所示的生产者消费者代码:

    使用多线程

    使用同步

    使用等待唤醒机制

    多人生产,多人消费



代码:

package org.yla.msb.day06;

/**
* 生产者消费者多线程例子 编写一个程序 有多人生产馒头 多人吃馒头,有个篮子可以盛放馒头 面向对象分析:类 馒头类 篮子类 生产者类 消费者类
*
* @author yangluan 2016年11月1日下午9:08:20
*/
public class ProducterComsuterDemo {

public static void main(String[] args) {
Bucket bc = new Bucket();
Producter p = new Producter(bc);
Cosumter m = new Cosumter(bc);
new Thread(p,"生产者A: ").start();
new Thread(p,"生产者B: ").start();
new Thread(m,"消费者A: ").start();
}
}

/**
* 馒头类
*
* @author yangluan 2016年11月1日下午9:10:00
*/
class ManTou {
private int id; // 馒头编号

public ManTou(int id) {
this.id = id;
}

@Override
public String toString() {
return "馒头" + id;
}
}

/**
* 篮子类 分析 篮子里面可以存放固定的馒头 应该是数组存放 存放馒头的时候是有序的 采用栈 先进后的方式存储 篮子方法:可以放馒头
* 可以取馒头,但是放和取的时候是有编号的,所以应该有个标记为index
*
* @author yangluan 2016年11月1日下午9:11:21
*/
class Bucket {
private int index = 0; // 篮子放馒头标记位
private ManTou[] arrManTou = new ManTou[6]; // 可以存放6个馒头

// 放馒头方法
public synchronized void push(ManTou m) { // 只能有一个人在生产馒头修改标记位共享变量index
while (index == arrManTou.length) { // 说明馒头放满了,就需要等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}

}
this.notifyAll(); // 唤醒消费馒头的线程
// 开始生产馒头
arrManTou[index] = m;
index++;
System.out.println(Thread.currentThread().getName() + "生产" + m);

}

// 取馒头方法
public synchronized ManTou pop() { // 只能有一个人在取馒头修改标记位共享变量index
while (index == 0) {// 篮子为空 开始等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();// 唤醒所有生产馒头的线程
// 开始消费
index--;
System.out.println(Thread.currentThread().getName() + "消费"
+arrManTou[index] );
return arrManTou[index];
}
}

/**
* 生产者类
* 可以看见篮子
* 可以生产馒头
* @author yangluan
* 2016年11月1日下午9:26:29
*/
class Producter implements Runnable{
private Bucket bc =null;

public Producter(Bucket bc){
this.bc =bc;
}

@Override
public void run() {
for (int i = 0; i < 10; i++) {
ManTou m =new ManTou(i);
bc.push(m);
try {
Thread.sleep((int)(Math.random()* 100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

/**
* 消费类
* 可以看见篮子
* 可以生产馒头
* @author yangluan
* 2016年11月1日下午9:26:29
*/
class Cosumter implements Runnable{
private Bucket bc =null;

public Cosumter(Bucket bc){
this.bc =bc;
}

@Override
public void run() {
for (int i = 0; i < 20; i++) {
ManTou m =this.bc.pop();
try {
Thread.sleep((int)(Math.random()* 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


运行:

生产者B: 生产馒头0
生产者A: 生产馒头0
消费者A: 消费馒头0
生产者A: 生产馒头1
生产者A: 生产馒头2
生产者B: 生产馒头1
生产者A: 生产馒头3
生产者A: 生产馒头4
消费者A: 消费馒头4
生产者A: 生产馒头5
消费者A: 消费馒头5
生产者B: 生产馒头2
消费者A: 消费馒头2
生产者B: 生产馒头3
消费者A: 消费馒头3
生产者B: 生产馒头4
消费者A: 消费馒头4
生产者B: 生产馒头5
消费者A: 消费馒头5
生产者B: 生产馒头6
消费者A: 消费馒头6
生产者B: 生产馒头7
消费者A: 消费馒头7
生产者B: 生产馒头8
消费者A: 消费馒头8
生产者B: 生产馒头9
消费者A: 消费馒头9
生产者A: 生产馒头6
消费者A: 消费馒头6
生产者A: 生产馒头7
消费者A: 消费馒头7
生产者A: 生产馒头8
消费者A: 消费馒头8
生产者A: 生产馒头9
消费者A: 消费馒头9
消费者A: 消费馒头3
消费者A: 消费馒头1
消费者A: 消费馒头2
消费者A: 消费馒头1
消费者A: 消费馒头0


本人地址:http://blog.csdn.net/hblfyla/article/details/53001523
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: