黑马程序员技术交流社区
标题:
用多线程 做了半个 杀人游戏 哈哈
[打印本页]
作者:
mohuancaizi
时间:
2014-3-22 13:43
标题:
用多线程 做了半个 杀人游戏 哈哈
package duoxiancheng;
class person implements Runnable{
int person = 1000;
Object obj =new Object();
public void run(){
while(true){
synchronized (obj) {
if (person > 0) {
System.out.println("杀手"+Thread.currentThread().getName() + " 正在杀 "
+"第" +person--+"个人");
}
else{
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("杀人完毕!!!!!!!!!!");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("天亮了!!!!!请睁眼!!!!!!");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("警察请指认");
try {
Thread.sleep(5100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("死了"+"1000"+"人"+"我也没办法");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("我还是先回睡觉去!!!!~~~~~~");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("拜拜");
System.exit(person);
}
}
}
}
}
public class KillPersonGame {
public static void main(String[] args) {
System.out.println("天黑了,请闭眼,杀手请杀人");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
person td = new person();
Thread td1=new Thread(td);
Thread td2=new Thread(td);
Thread td3=new Thread(td);
Thread td4=new Thread(td);
Thread td5=new Thread(td);
td1.start();
td2.start();
td3.start();
td4.start();
td5.start();
}
}
复制代码
作者:
Asura
时间:
2014-5-7 07:04
顶一下!
作者:
孟凡超
时间:
2014-5-7 07:38
不容易,顶一个
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2