龟兔赛跑(通过多线程来实现 里面的具体方法)

TT.java

package com.lanqiao.demo3;

/**
* 乌龟
* @author Administrator
*
*/
public class TT extends Thread {
public TT(String name) {
super(name);
}
@Override
public void run() {
try {
for (int i = 1; i <= 100;i++ ) {
Thread.sleep(10);
System.out.println(this.getName()+"跑了"+i+"米");
}
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println(this.getName()+"输了");
} }

RR.java

package com.lanqiao.demo3;

/**
* 兔子
*
* @author Administrator
*
*/
public class RR extends Thread {
public RR(String name) {
super(name);
}
@Override
public void run() {
try {
System.out.println("比赛开始");
for (int i = 1; i <= 100;i++) {
Thread.sleep(10);
Method(i);
System.out.println(this.getName()+"跑了"+i+"米");
}
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println("兔子跑完了");
} //规则
public void Method(int i) {
try {
if(i == 10){
Thread.sleep(15); } if(i==50){
Thread.sleep(1);
} if(i==80){
Thread.sleep(10);
}
} catch (Exception e) {
e.printStackTrace();
} }
}

Test.java

package com.lanqiao.demo3;

/**
* 龟兔赛跑(通过线程来实现)
*
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
//System.out.println("11");
TT t = new TT("乌龟");
t.start();
RR r = new RR("兔子");
r.start();
//System.out.println("游戏结束"+t.getName());
}
}
05-11 20:57