




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验实验 7 7 多线程多线程 实验目的实验目的 1 掌握 Java 多线程的概念和实现 2 掌握使用继承 Thread 产生线程 3 实现 Runnable 接口产生线程 4 掌握 wait notify 机制 任务一 任务一 多线程输出字符串 继承多线程输出字符串 继承 Thread 类 类 创建两个独立的线程 一个线程打印 20 次 A 另一个线程打 印 20 次 B 输出结果类似于 ABBABAABBA 两个独立线程 分别输出 A 和 B 要求 使用继承 Thread 类的方式实现线程对象 代码如下 A java packagepackage first publicpublic classclass A extendsextends Thread publicpublic voidvoid run forfor intint i 1 i 20 i System outout print A B java packagepackage first publicpublic classclass B extendsextends Thread publicpublic voidvoid run forfor intint i 1 i 20 i System outout print B Test java packagepackage first publicpublic classclass test publicpublic staticstatic voidvoid main String args A a B b a newnew A b newnew B a start b start 任务二 任务二 多个线程共同执行目标对象的任务 实现 Runnable 有一批零件 Part 的生产任务 总量是 800 个 生产一个 零件耗时 10ms 有 4 个车间 Workshop 共同完成这一批任 务 分别统计每个车间各自生产了多少个零件并输出 代码如下 packagepackage second publicpublic classclass Part publicpublic staticstatic voidvoid main String args Thread w1 Thread w2 Thread w3 Thread w4 Workspace ws ws newnew Workspace w1 newnew Thread ws w2 newnew Thread ws w3 newnew Thread ws w4 newnew Thread ws w1 setName WorkSpace1 w2 setName WorkSpace2 w3 setName WorkSpace3 w4 setName WorkSpace4 w1 start w2 start w3 start w4 start trytry Thread sleep 2500 catchcatch InterruptedException e ws print classclass Workspace implementsimplements Runnable intint partamount 800 intint c1 0 intint c2 0 intint c3 0 intint c4 0 publicpublic voidvoid run whilewhile truetrue String name Thread currentThread getName ifif name equals WorkSpace1 change c1 c1 1 ifif name equals WorkSpace2 change c2 c2 1 ifif name equals WorkSpace3 change c3 c3 1 ifif name equals WorkSpace4 change c4 c4 1 trytry Thread sleep 10 catchcatch InterruptedException e ifif partamount 0 returnreturn publicpublic voidvoid print System outout println WorkSpace1 c1 System outout println WorkSpace2 c2 System outout println WorkSpace3 c3 System outout println WorkSpace4 c4 publicpublic synchronizedsynchronized voidvoid change partamount partamount 1 任务三 任务三 生产者生产者 消费者问题 线程间通信 消费者问题 线程间通信 有一种商品 Product 类 一个货架类 GoodsShelf 拥有 一个容量为 size 可根据需要设定大小 的 Product 数组 生产 者 Producer 往货架上放置商品 消费者 Consumer 从货 架上消费商品 二者是并发执行的 货架空的时候只能生产 货架 满的时候只能消费 使用多线程并发机制和线程间通信 模拟商品 编号从 1 到 100 的生产和消费过程 要求 必做 只模拟一个生产者和一个消费者的情况 选作 模拟 2 个生产者和 3 个消费者的情况 生产商品的时间和 消费商品的时间可以调整 Thread sleep 整型值 Product java packagepackage third publicpublic classclass Product intint amount Product intint amount thisthis amount amount publicpublic String toString returnreturn Product amount GoodsShelf java packagepackage third publicpublic classclass GoodsShelf intint front 0 Product p newnew Product 10 publicpublic synchronizedsynchronized voidvoid push Product o whilewhile front 10 trytry thisthis wait catchcatch InterruptedException e System outout println Error e thisthis notify p front o thisthis front publicpublic synchronizedsynchronized Product p whilewhile front 0 trytry thisthis wait catchcatch InterruptedException e System outout println Error e thisthis notify thisthis front returnreturn p front Product java packagepackage third publicpublic classclass Producer implementsimplements Runnable GoodsShelf g nullnull Producer GoodsShelf g thisthis g g Override publicpublic voidvoid run TODOTODO 自动生成的方法存根 forfor intint i 0 i 100 i Product pt newnew Product i g push pt System outout println Produced pt trytry Thread sleep 10 catchcatch InterruptedException e System outout println Error e Consumer java packagepackage third publicpublic classclass Consumer implementsimplements Runnable GoodsShelf g nullnull publicpublic Consumer GoodsShelf g supersuper thisthis g g Override publicpublic voidvoid run TODOTODO 自动生成的方法存根 forfor intint i 0 i 100 i Product pt g pop System outout println Consumed pt trytry Thread sleep 100 catchcatch InterruptedException e System outout println Error e Test java packagepackage third publicpublic classclass test publicpublic staticstatic vo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 魔术剪辑基础知识培训课件
- 济南市2024-2025学年八年级下学期语文期末模拟试卷
- 电路故障诊断与处理
- 电赛知识培训课件
- 电脑公司基本知识培训课件
- rgp考试题及答案
- pfmea考试题及答案
- 电缆绞磨机课件
- 高空培训课件
- HET0016-Standard-生命科学试剂-MCE
- 4.《花之歌》教学设计-2024-2025学年统编版语文六年级上册
- 危险废弃物管理培训试题(附答案)
- 2025国投生物制造创新研究院有限公司招聘(31人)考试备考试题及答案解析
- 新学期,新征程+课件-2025-2026学年高二上学期开学第一课主题班会
- 2025新版企业员工劳动合同范本
- PCR实验室基因扩增检验人员培训试题及答案
- 赛事租赁用品租赁模式分析报告
- 2025年全国版图知识竞赛(中学组)历年参考题库含答案详解(5卷)
- 2025年西藏自治区三支一扶人员招募考试(公共基础知识)历年参考题库含答案详解(5卷)
- 护士长领导力提升与团队管理技巧
- 2025年富县辅警考试题库(附答案)
评论
0/150
提交评论