2010813068JAVA实验8报告.ppt.docVIP

  • 2
  • 0
  • 约14.81万字
  • 约 26页
  • 2017-08-10 发布于河南
  • 举报
2010813068JAVA实验8报告.ppt

集美大学计算机工程学院实验报告 课程名称:丁跃潮 班级:软件1012 实验成绩: 指导教师:丁跃潮 姓名:蔡荣杰 实验项目名称:实验8 学号:2010813068 上机实践日期: 实验项目编号:8 组号: 上机实践时间: 学时 一、目的 本次实验所涉及并要求掌握的知识点。 线程,接口,继承, 实验内容 两个警察抓一个小偷 设计和编码 实现四个线程,分别为警察1,警察2,小偷,电脑, 调用run()方法,记录警察1,警察2,小偷在每秒所处位置,当警察与小偷相遇时,玩家胜并结束程序, 如果玩家无法在500秒内,完成抓捕任务时,玩家失败,游戏结束。 使用继承Thread类来实现; import java.util.concurrent.*; public class CatchStealer1 extends Thread/继承Thread/ { Police3 polic1=new Police3(); Police4 polic2=new Police4(); Stealer1 stealr=new Stealer1(); Thread police1=new Thread(polic1);/设计三个线程/ Thread police2=new Thread(polic2); Thread stealer=new Thread(stealr); private int xpol1; private int xpol2; private int xstea; private int ypol1; private int ypol2; private int ystea; public CatchStealer1() throws Exception { police1.setDaemon(true); police1.start(); police2.setDaemon(true); police2.start(); stealer.setDaemon(true); stealer.start(); } public void run() { try { while(true) { TimeUnit.SECONDS.sleep(1);/同步记录警察1,警察2,小偷的位置/ xpol1=polic1.getXlong(); ypol1=polic1.getYlong(); xpol2=polic2.getXlong(); ypol2=polic2.getYlong(); xstea=stealr.getXlong(); ystea=stealr.getYlong(); if(((xstea==xpol1)(ystea==ypol1))||((xstea==xpol2)(ystea==ypol2))) { System.out.println(YOUR WIN); System.exit(0); } } } catch (InterruptedException e){ System.out.println(sleep() interrupted); }/抛出异常/ } public static void main(String[] args) throws Exception { CatchStealer1 jk=new CatchStealer1();/设计电脑线程/ jk.setDaemon(true);/设为后台/ jk.start(); TimeUnit.SECONDS.sleep(500); System.out.println(游戏失败!); } } 使用实现Runnable接口来实现; 代码解析与CatchStealer1相似 import java.util.concurrent.*; public class CatchStealer extends Thread { Police3 polic1=new Police3(); Police4 polic2=new Police4(); Stealer1 stealr=new Stealer1(); Thread police1=new Thread(polic1); Thread police2=new Thread(polic2); Thr

文档评论(0)

1亿VIP精品文档

相关文档