



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
包为com.app PublicResource为功能资源类看类名可以理解!package com.app;public class PublicResource private int number =0;/* * 增加 */public synchronized int increase()while(number=20)try System.out.println(警告!仓库满了);this.wait(); catch (Exception e) e.printStackTrace();number+;this.notify();return number;/* * 减少 */public synchronized int decrease()while(number=0)try System.out.println(警告!仓库空了);this.wait(); catch (InterruptedException e) e.printStackTrace(); number-; this.notify(); return number;生产者类package com.app;public class Producer implements Runnablepublic PublicResource publicResource;public Producer(PublicResource rs)this.publicResource =rs;public void run() while(true)try Thread.sleep(long)(Math.random()*200); catch (InterruptedException e) e.printStackTrace();int number = this.publicResource.increase();System.out.println(Thread.currentThread().getName()+生产一个剩余数量为:+number);消费者类package com.app;public class Consumer implements Runnablepublic PublicResource publicResource;public Consumer(PublicResource rs)this.publicResource =rs;public void run() while(true)try Thread.sleep(long)(Math.random()*200); catch (InterruptedException e) e.printStackTrace();int number =this.publicResource.decrease();System.out.println(Thread.currentThread().getName()+消费一个剩余数量为:+number);主类测试用package com.app;public class Test public static void main(String args) PublicResource pr = new PublicResource();Producer pro1 = new Producer(pr);Thread th1 = new Thread(pro1);th1.setName(生产者1号);th1.start();Producer pro2 = new Producer(pr);Thread th2 = new Thread(pro2);th2.setName(生产者2号);th2.start();Producer pro3 = new Producer(pr);Thread th3 = new Thread(pro3);th3.setName(生产者3号);th3.start();Consumer con1 = new Consumer(pr);Thread th4 = new Thread(con1);th4.setName(消费者1号);th4.start();Consumer con2 = new Consumer(pr);Thread th5 = new Thread(con2);th5.setName(消费者2号);th5.start();Consumer con3 = new Consumer(pr);Thread th6 = new Thread(co
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 旅游景点推广活动计划
- 城市轨道交通站点周边开发项目2025年社会稳定风险评估及风险管理策略
- 2025年新能源汽车动力电池技术创新与应用场景报告
- 工业互联网平台安全多方计算在智能工厂网络安全架构中的数据安全控制报告
- 安全教育培训经过考核课件
- 特色主题餐厅2025年餐饮行业风险防范与市场发展研究报告
- 新能源汽车2025轻量化材料创新与碰撞安全性能评估报告
- 物业管理行业社区金融服务与增值业务拓展分析报告
- 沼气技术创新在2025年有机农业中的应用报告
- 新能源行业2025年质量认证技术创新与可再生能源认证报告
- 记叙文中的并列式结构课件
- 新媒体运营实务完整全套课件
- 桩基础平法施工图(平法施工图识读)
- GB/T 9113-2010整体钢制管法兰
- GB/T 23338-2018内燃机增压空气冷却器技术条件
- 海姆立克急救法完整版本课件
- 国家地表水环境质量监测网采测分离实施方案课件
- 控压钻井技术及实践培训讲义工艺课件
- 厚度仪点检表
- 北京市水利工程维修养护定额
- 自然拼读法在小学英语教学中的应用的实践研究
评论
0/150
提交评论