黑马程序员技术交流社区

标题: 用多线程 做了半个 杀人游戏 哈哈 [打印本页]

作者: mohuancaizi    时间: 2014-3-22 13:43
标题: 用多线程 做了半个 杀人游戏 哈哈
  1. package duoxiancheng;

  2. class person implements Runnable{
  3.        
  4.         int person = 1000;
  5.         Object obj =new Object();  
  6.         public void run(){
  7.                 while(true){
  8.                        
  9.                         synchronized (obj) {
  10.                                 if (person > 0) {
  11.                                         System.out.println("杀手"+Thread.currentThread().getName() + "   正在杀  "
  12.                                                         +"第" +person--+"个人");
  13.                                 }
  14.                                 else{
  15.                                         try {
  16.                                                 Thread.sleep(5000);
  17.                                         } catch (InterruptedException e) {
  18.                                                 // TODO Auto-generated catch block
  19.                                                 e.printStackTrace();
  20.                                         }
  21.                                         System.out.println("杀人完毕!!!!!!!!!!");
  22.                                         try {
  23.                                                 Thread.sleep(5000);
  24.                                         } catch (InterruptedException e) {
  25.                                                 // TODO Auto-generated catch block
  26.                                                 e.printStackTrace();
  27.                                         }
  28.                                         System.out.println("天亮了!!!!!请睁眼!!!!!!");
  29.                                         try {
  30.                                                 Thread.sleep(5000);
  31.                                         } catch (InterruptedException e) {
  32.                                                 // TODO Auto-generated catch block
  33.                                                 e.printStackTrace();
  34.                                         }
  35.                                         System.out.println("警察请指认");
  36.                                         try {
  37.                                                 Thread.sleep(5100);
  38.                                         } catch (InterruptedException e) {
  39.                                                 // TODO Auto-generated catch block
  40.                                                 e.printStackTrace();
  41.                                         }
  42.                                         System.out.println("死了"+"1000"+"人"+"我也没办法");
  43.                                         try {
  44.                                                 Thread.sleep(5000);
  45.                                         } catch (InterruptedException e) {
  46.                                                 // TODO Auto-generated catch block
  47.                                                 e.printStackTrace();
  48.                                         }
  49.                                        
  50.                                         System.out.println("我还是先回睡觉去!!!!~~~~~~");
  51.                                         try {
  52.                                                 Thread.sleep(5000);
  53.                                         } catch (InterruptedException e) {
  54.                                                 // TODO Auto-generated catch block
  55.                                                 e.printStackTrace();
  56.                                         }
  57.                                         System.out.println("拜拜");
  58.                                         System.exit(person);
  59.                                 }
  60.                         }
  61.                        
  62.                        
  63.                 }
  64.                
  65.         }
  66. }
  67. public class KillPersonGame {

  68.        
  69.         public static void main(String[] args) {
  70.                 System.out.println("天黑了,请闭眼,杀手请杀人");
  71.                 try {
  72.                         Thread.sleep(5000);
  73.                 } catch (InterruptedException e) {
  74.                        
  75.                         e.printStackTrace();
  76.                 }
  77.                 person td = new person();
  78.                 Thread td1=new Thread(td);
  79.                 Thread td2=new Thread(td);
  80.                 Thread td3=new Thread(td);
  81.                 Thread td4=new Thread(td);
  82.                 Thread td5=new Thread(td);
  83.                
  84.                
  85.                 td1.start();
  86.                 td2.start();
  87.                 td3.start();
  88.                 td4.start();
  89.                 td5.start();
  90.                
  91.         }

  92. }
复制代码

作者: Asura    时间: 2014-5-7 07:04
顶一下!
作者: 孟凡超    时间: 2014-5-7 07:38
不容易,顶一个




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2