




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、网络编程实验报告实验题目:生产者 / 消费者问题姓名:蔡锋学号: 0907300230 班级:信息安全 091 班完成时间:2011.10.252011.11.05 指导老师 : 张小萍一、实验概述11 实验目的了解进程的同步、互斥机制,认识理解其过程,并用于解决生产者/ 消费者问题。使用 java编程实现 “ 生产者 消费者 ” 多线程同步问题。12 任务描述有两个生产者, 每个生产者每次可以生产1 个产品,有一个消费者每次消费2 个产品,仓库最多只能存放2 个产品,若仓库满,则生产者需等待空位来存放产品,若仓库产品数量不足, 则消费者需等待来消费产品。请用多线程技术实现以上功能,要求生产者
2、存放产品时按先来先服务的队列原则来存放产品。二、功能及处理逻辑设计2.1 具体要求1. 三个线程。t0 ,t1 分别代表生产者 a,生产者 b。t2 代表消费者 consumer 2. 一个仓库(缓冲区),最大存量为两个产品21 功能及模块设计2.2 特别功能1. thread.sleep(int)(math.random()方法令线程随机发生。2. 用 synchronized来实现同步,当仓库满时,等待消费线程,若发生生产者线程则立即等待, 进入沉睡状态, 当消费线程发生时, 同时逐一激活已沉睡生产进程,实现先来先服务。3. 用 synchronized来实现同步,当仓库未满时,发生消费线
3、程,则等待(睡眠) ,当发生线程,使仓库数为2,激活消费线程。4. 定义一个新线程必须重写父类的run 方法。2.3 算法搭建三、源代码及方法解释(重点步骤已用红色注释)本次方法中未用到队列,利用的是多次调用线程,逐一实现先来先服务import java.util.*; public class xiancheng public static void main(string args) storehouse storehouse = new storehouse(); producer producer1 = new producer(生产者 a, storehouse); producer
4、 producer2 = new producer(生产者 b, storehouse); consumer comsumer = new consumer( 消费者 , storehouse); thread t0 = new thread(producer1); thread t1 = new thread(producer2); thread t2 = new thread(comsumer); 生产者 1 消费者仓 库是 否 满生产者 2 生产者 1 等待消费者消费生产者 2 等待y 生产者 b 生产n 生产者 a 生产n 位 于 等待 第 一位位于等待第一位pause 结束t0.st
5、art(); t1.start(); t2.start(); class producer extends thread implements runnable / 定义生产者线程 private string producername = null; private storehouse storehouse = null; public producer(string producername, storehouse storehouse) ducername = producername; this.storehouse = storehouse; / 声明生产者要放产品
6、的仓库public void setproducername(string producername) ducername = producername; public string getproducername() return producername; public void run()/ 定义一个新线程必须重写父类的run 方法 while (true) storehouse.store_in(this); try thread.sleep(int)(math.random() * 2500); / 定义线程休眠时间catch (interruptedexceptio
7、n e) return; / 有可能抛出异常,必须对它进行捕捉 class consumer extends thread implements runnable / 定义消费者线程public string consumername = null; public storehouse storehouse = null; public consumer(string consumername, storehouse storehouse) this.consumername = consumername; this.storehouse = storehouse; public void s
8、etconsumername(string consumername) this.consumername = consumername; public string getconsumername() return consumername; public void run() while (true) storehouse.store_out(this); try thread.sleep(int)(math.random() * 5000); / 定义线程休眠时间catch (interruptedexception e) return; class storehouse int cou
9、nt = 0;/ 创建仓库,用来放产品public synchronized void store_in(producer pro) while (count = 2) /仓库容量为 2 system.out.println(仓库已满 , + pro.getproducername() + 正等待生产 .); try this.wait();/ 生产者线程进入沉睡 catch (interruptedexception e) count+;/产品数自加 1 system.out.println(pro.getproducername() + 生产了 1个产品 ,库存为 + count); /
10、激活等待的线程,实现先来先服务notify(); public synchronized void store_out(consumer con) while (count = 0 | count = 1) try system.out.println(仓库数量不足,消费者等待消费.); this.wait(); catch (interruptedexception e) count = count-2; system.out.println(con.getconsumername() + 消费了 2 个产品,库存为 + count); notify(); 四、程序截图1.先来先服务效果2.消费者等待情况消费者等待的前提下,当仓
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 房屋价格管理办法
- 建筑采暖管理办法
- 杭州离职管理办法
- 成品线材管理办法
- 救助管理办法讲座
- 拍卖变卖管理办法
- 摄影比赛管理办法
- 拆除垃圾管理办法
- 撤销部门管理办法
- 微信舆情管理办法
- FZ/T 01082-2017粘合衬干热尺寸变化试验方法
- 湖北大学物理化学试题及答案(B)
- 幼儿园语言领域专题讲座课件
- QES三体系内审检查表 含审核记录
- 湖南省人民医院进修申请表
- 3、《广播电视音像资料编目规范-电视资料部分》标准设计及应用说明【精选文档】
- GB∕T 24635.4-2020 产品几何技术规范(GPS) 坐标测量机(CMM)确定测量不确定度的技术 第4部分:应用仿真技术评估特定任务的测量不确定度
- Q∕SY 05357-2020 油气管道地面标识设置规范
- 2022年福建省厦门市中考语文质检试卷(5月份)
- 煤炭行业信息化解决方案
- 压力性损伤手册
评论
0/150
提交评论