马士兵-多线程学习第05课 生产者消费者问题
2016-11-01 21:58
447 查看
本人地址:http://blog.csdn.net/hblfyla/article/details/53001523
要求实现如下如所示的生产者消费者代码:
使用多线程
使用同步
使用等待唤醒机制
多人生产,多人消费
代码:
运行:
本人地址: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
相关文章推荐
- JAVA多线程学习--生产者消费者问题
- C++多线程学习:生产者消费者问题
- Linux C/C++多线程学习:生产者消费者问题
- JAVA学习第二十七课(多线程(六))- 多生产者多消费者问题(JDK1.5新特性)
- java 多线程学习之多生产者多消费者产生的线程安全问题分析与解决:Lock和Condition
- JAVA学习第二十七课(多线程(六))- 多生产者多消费者问题(JDK1.5新特性)
- Java多线程学习之生产者消费者问题(一)
- 多线程:生产者与消费者经典问题(马士兵)
- java 中多线程生产者消费者问题详细介绍
- 学习多线程的生产者和消费者系列的相关实践
- 多线程+阻塞队列实现生产者-消费者模型获取队列数据问题
- Java——生产者消费者中多线程安全问题
- 多线程生产者与消费者问题代码模型
- 多线程经典——生产者消费者问题(未加锁版)
- 多线程经典——生产者消费者问题(加锁版)
- 生产者和消费者问题学习以及Java实现
- java代码实现多线程消费者生产者问题。
- java多线程之生产者和消费者经典问题案例
- 29-多线程(线程间通信-多生产者多消费者问题-JDK1.5新特性-Condition).1 2 31-多线程(线程间通信-多生产者多消费者问题-JDK1.5解决办法-范例). 1 2
- 【java 多线程】多线程并发同步问题及生产者、消费者问题