版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、深 圳 大 学 实 验 报 告课程名称: Java程序设计 实验项目名称: 线程 学院: 计算机与软件学院 专业: 指导教师: 谭舜泉 报告人: 学号: 班级: 3班 实验时间: 2012年12月13日 实验报告提交时间: 2012年12月26日 教务部制一、实验内容:8.2.3 吵醒休眠的线程实验代码:class Shop implements RunnableThread zhangWorker,wangWorker,boss;Shop()boss=new Thread(this);/创建boss,Shop对象为boss目标对象zhangWorker=new Thread(this);wa
2、ngWorker=new Thread(this);zhangWorker.setName(张工);wangWorker.setName(王工);boss.setName(老板);public void run()int i=0;if(Thread.currentThread()=zhangWorker)while(true)tryi+;System.out.printf(n%s已搬运了%d箱苹果n,zhangWorker.getName(),i);if(i=3)return;Thread.sleep(10000);/zhangWorker休眠10秒catch(InterruptedExcep
3、tion e)System.out.printf(n%s让%s继续工作,boss.getName(),zhangWorker.getName();else if(Thread.currentThread()=wangWorker)while(true)tryi+;System.out.printf(n%s已搬运了%d箱香蕉n,wangWorker.getName(),i);if(i=3)return;Thread.sleep(10000);catch(InterruptedException e)System.out.printf(n%s让%s继续工作,boss.getName(),wangW
4、orker.getName();else if(Thread.currentThread()=boss)while(true)zhangWerrupt();/吵醒zhangWorkerwangWerrupt();if(!(wangWorker.isAlive()|zhangWorker.isAlive()System.out.printf(%n%s下班,boss.getName();return; public class ShopExamplepublic static void main(String args)Shop shop=new Shop();
5、shop.zhangWorker.start();shop.wangWorker.start();shop.boss.start();实验截图:8.2.4 排队买票实验代码:class TicketSeller/负责卖票的类int fiveNumber=1,tenNumber=0,twentyNumber=0;public synchronized void sellTicket(int receiveMoney,int buyNumber)if(receiveMoney=5)fiveNumber=fiveNumber+1;System.out.printf(n%s给我5元钱,这是您的1张入场
6、券,Thread.currentThread().getName();else if(receiveMoney=10&buyNumber=2)tenNumber=tenNumber+1;System.out.printf(n%s给我10元钱,这是您的2张入场券,Thread.currentThread().getName();else if(receiveMoney=10&buyNumber=1)while(fiveNumber1)/给出线程等待的条件trySystem.out.printf(n%s靠边等,Thread.currentThread().getName();wait();/线程进
7、入等待状态System.out.printf(n%30s结束等待状态n,Thread.currentThread().getName();catch(InterruptedException e)fiveNumber=fiveNumber-1;tenNumber=tenNumber+1;System.out.printf(n%s给我10元钱,找您5元钱,这是您的1张入场券,Thread.currentThread().getName();else if(receiveMoney=20&buyNumber=1)while(fiveNumber1|tenNumber1)/给出线程需要等待的条件tr
8、ySystem.out.printf(n%30s靠边等,Thread.currentThread().getName();wait();/线程进入等待状态System.out.printf(n%30s结束等待,Thread.currentThread().getName();catch(InterruptedException e)fiveNumber=fiveNumber-1;tenNumber=tenNumber-1;twentyNumber=twentyNumber+1;System.out.printf(n%s给我20元钱,找您一张5元和一张10元,这是您的1张入场券,Thread.c
9、urrentThread().getName();else if(receiveMoney=20&buyNumber=2)while(tenNumber1)/给出线程等待的条件trySystem.out.printf(n%30s靠边等n,Thread.currentThread().getName();wait();/线程进入的等待状态System.out.printf(n%30s结束等待,Thread.currentThread().getName();catch(InterruptedException e)tenNumber=tenNumber-1;twentyNumber=twenty
10、Number+1;System.out.printf(n%s给我20元钱,找您一张10元,这是您的2张入场券,Thread.currentThread().getName();notifyAll();/通知等待的线程结束等待class Cinema implements RunnableThread zhao,qian,sun,li,zhou;TicketSeller seller;Cinema()zhao=new Thread(this);qian=new Thread(this);sun=new Thread(this);li=new Thread(this);zhou=new Threa
11、d(this);zhao.setName(赵);qian.setName(钱);sun.setName(孙);li.setName(李);zhou.setName(周);seller=new TicketSeller();public void run()if(Thread.currentThread()=zhao)seller.sellTicket(20,2);else if(Thread.currentThread()=qian)seller.sellTicket(20,1);else if(Thread.currentThread()=sun)seller.sellTicket(10,1
12、);else if(Thread.currentThread()=li)seller.sellTicket(10,2);else if(Thread.currentThread()=zhou)seller.sellTicket(5,1); public class SaleExamplepublic static void main(String args)Cinema cinema=new Cinema();cinema.zhao.start();tryThread.sleep(1000);catch(InterruptedException e)cinema.qian.start();tr
13、yThread.sleep(1000);catch(InterruptedException e)cinema.sun.start();tryThread.sleep(1000);catch(InterruptedException e)cinema.li.start();tryThread.sleep(1000);catch(InterruptedException e)cinema.zhou.start();实验截图:二、实验体会:对于实验“吵醒睡眠的线程”,让我更熟悉了interrupt()方法的使用,interrupt()方法常用来“吵醒”休眠的线程。当一些线程调用了sleep()方法处于休眠状态时,一个只用CPU资源的其他线程在执行的过程中,可以让休眠的线程分别调用interrupt()方法“吵醒”自己,即导致休眠的线程发生InterruptedException异常,从而结束休眠,重新排队等待CPU资源。打完代码后,反复运行程序,发现每次程序运行后输出的结果都不一样,可能是运行过程中间断的时间比较短,间隔时间稍长一点,结果就不会变的这么频繁了。对于实验“排队买票”,更多的使用了wait()、notify()和notifyAll()方法。对3个方法的解释:一
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年新疆交通职业技术学院高职单招职业技能测试近5年常考版参考题库含答案解析
- 2025年人教五四新版选择性必修三化学下册阶段测试试卷
- 2025年牛津上海版必修1历史下册阶段测试试卷含答案
- 2025年粤教版九年级历史下册月考试卷含答案
- 服务项目明细协议书(2篇)
- 2025年外研衔接版必修2化学上册月考试卷含答案
- 2025年湘师大新版必修2历史上册月考试卷含答案
- 中国纳米材料行业分析报告:产业链全景、发展环境、进出口贸易、需求规模(2024版)
- 2025年粤人版八年级地理下册阶段测试试卷
- 2025年广西卫生职业技术学院高职单招职业适应性测试近5年常考版参考题库含答案解析
- 2023-2024年员工三级安全培训考试题及参考答案(综合题)
- 对口升学语文模拟试卷(6)-江西省(解析版)
- 2025保安部年度工作计划
- 2024年江苏经贸职业技术学院单招职业适应性测试题库
- 招标采购基础知识培训
- 电力系统分布式模型预测控制方法综述与展望
- 2024年注册建筑师-二级注册建筑师考试近5年真题附答案
- 五年级口算题卡每天100题带答案
- 2024年贵州省中考理科综合试卷(含答案)
- 无人机技术与遥感
- 燃煤电厂超低排放烟气治理工程技术规范(HJ 2053-2018)
评论
0/150
提交评论